/*
 *  iPhoneFilter.c
 *  test2
 *
 *  Created by BoHyun Jo on 10/30/08.
 *  Copyright 2008 Hansung. All rights reserved.
 *
 */

#include "iPhoneFilter.h"
#define RG_SIZE 3
#define NN 10

#define NF 8
int reg[9][9];
int rgox[9];
int rgoy[9];

int imgR[25],imgG[25],imgB[25];
int getVal(int ed,int val,int con)
{
	int a,b;
	int v2;
	
	if(ed>255) ed=255;
	v2=(255-128)*val/255+128;
	a=255-v2;
	b=-a*ed/255+255;
	b=(b*con+val*(100-con))/100;
	return b;
} 
// ev[gÝè
void setRegInfo()
{
	reg[0][0]=1;    reg[0][1]=1;    reg[0][2]=0;
	reg[0][3]=1;    reg[0][4]=1;    reg[0][5]=1;
	reg[0][6]=0;    reg[0][7]=1;    reg[0][8]=1;
	rgox[0]=-2;
	rgoy[0]=-2;
	
	reg[1][0]=1;    reg[1][1]=1;    reg[1][2]=1;
	reg[1][3]=1;    reg[1][4]=1;    reg[1][5]=1;
	reg[1][6]=0;    reg[1][7]=1;    reg[1][8]=0;
	rgox[1]=-1;
	rgoy[1]=-2;
	
	reg[2][0]=0;    reg[2][1]=1;    reg[2][2]=1;
	reg[2][3]=1;    reg[2][4]=1;    reg[2][5]=1;
	reg[2][6]=1;    reg[2][7]=1;    reg[2][8]=0;
	rgox[2]= 0;
	rgoy[2]=-2;
	
	reg[3][0]=0;    reg[3][1]=1;    reg[3][2]=1;
	reg[3][3]=1;    reg[3][4]=1;    reg[3][5]=1;
	reg[3][6]=0;    reg[3][7]=1;    reg[3][8]=1;
	rgox[3]= 0;
	rgoy[3]=-1;
	
	reg[4][0]=1;    reg[4][1]=1;    reg[4][2]=0;
	reg[4][3]=1;    reg[4][4]=1;    reg[4][5]=1;
	reg[4][6]=0;    reg[4][7]=1;    reg[4][8]=1;
	rgox[4]= 0;
	rgoy[4]= 0;
	
	reg[5][0]=0;    reg[5][1]=1;    reg[5][2]=0;
	reg[5][3]=1;    reg[5][4]=1;    reg[5][5]=1;
	reg[5][6]=1;    reg[5][7]=1;    reg[5][8]=1;
	rgox[5]=-1;
	rgoy[5]= 0;
	
	reg[6][0]=0;    reg[6][1]=1;    reg[6][2]=1;
	reg[6][3]=1;    reg[6][4]=1;    reg[6][5]=1;
	reg[6][6]=1;    reg[6][7]=1;    reg[6][8]=0;
	rgox[6]=-2;
	rgoy[6]= 0;
	
	reg[7][0]=1;    reg[7][1]=1;    reg[7][2]=0;
	reg[7][3]=1;    reg[7][4]=1;    reg[7][5]=1;
	reg[7][6]=1;    reg[7][7]=1;    reg[7][8]=0;
	rgox[7]=-2;
	rgoy[7]=-1;
	
	reg[8][0]=1;    reg[8][1]=1;    reg[8][2]=1;
	reg[8][3]=1;    reg[8][4]=1;    reg[8][5]=1;
	reg[8][6]=1;    reg[8][7]=1;    reg[8][8]=1;
	rgox[8]=-1;
	rgoy[8]=-1;
}

int getRegVal(int *img,int num,int *ave)
{
	int xx,yy;
	int iy;
	int sx,sy;
	int ex,ey;
	int tmp,cnt;
	int max,min;
	int adr;
	int val;
	
	sx=2+rgox[num];
	sy=2+rgoy[num];
	ex=sx+RG_SIZE;
	ey=sy+RG_SIZE;
	tmp=0;
	cnt=0;
	adr=0;
	max= -1;
	min= 256;
	iy=0;
	for(yy=sy;yy<ey;yy++) {
		for(xx=sx;xx<ex;xx++) {
			if(reg[num][adr]) {
				val=img[xx+yy*5];
				tmp+=val;
				cnt++;
				if(max<val) max=val;
				if(min>val) min=val;
			}
			adr++;
		}
	}
	*ave=(tmp/cnt);
	if(*ave>255) *ave=255;
	return max-min;
} 
int absi(int i)
{
	if(i<0) return -i;
	return i;
}

int linarPol(int c1,int c2,int c3,int c4,double xr,double yr)
{
	double d,e,f;
	int res;
	
	d=(double)c1*(1.0-xr)+(double)c3*xr;
	e=(double)c2*(1.0-xr)+(double)c4*xr;
	f=d*(1.0-yr)+e*yr;
	res=(f+0.5);
	if(res>255) res=255;
	else if(res<0) res=0;
	return res;
} 
int getPatVal(int *mat,int n,int x,int y)
{
	if(x<0 || y<0 || x>=n || y>=n) return 0;
	
	return mat[x+y*n];
} 
int linearInterpolate(int *mat,int n,double x1,double y1)
{



	int px,py;
	int nx,ny;
	double xrate,yrate;
	int col1,col2,col3,col4;
	
	nx=(int)(x1);
	ny=(int)(y1);
	px=(int)x1;
	py=(int)y1;
	xrate=x1-(double)px;
	yrate=y1-(double)py;
	col1=getPatVal(mat,n,px,py);
	col2=getPatVal(mat,n,px,py+1);
	col3=getPatVal(mat,n,px+1,py);
	col4=getPatVal(mat,n,px+1,py+1);
	return linarPol(col1,col2,col3,col4,xrate,yrate);
} 
int makeBlurPat(int n,int ang,int **x,int **y,int **val,int *sum)
{
	int *tmpx,*tmpy,*tmpv;
	int *mat;
	int msum,tsum;
	int xx,yy,i;
	int vv;
	int hf;
	double rad;
	double fx,fy,tx,ty;
	double sinT,cosT;
	
	hf=n/2;
	
	tmpx=malloc(sizeof(int)*n*n);
	tmpy=malloc(sizeof(int)*n*n);
	tmpv=malloc(sizeof(int)*n*n);
	mat=malloc(sizeof(int)*n*n);
	for(yy=0;yy<n;yy++) {
		for(xx=0;xx<n;xx++) {
			mat[xx+yy*n]=0;
		}
	}
	msum=0;
	for(xx=0;xx<n;xx++) {
		vv=xx+1;
		if(vv>hf+1) vv=0;
		mat[xx+hf*n]=vv;
		msum+=vv;
	}
	
	rad=(double)ang*(3.14159265)/180.0;
	sinT=sin(rad);
	cosT=cos(rad);
	
	i=0;
	tsum=0;
	for(yy=0;yy<n;yy++) {
		for(xx=0;xx<n;xx++) {
			tx=(double)(xx-hf);
			ty=(double)(yy-hf);
			fx=  cosT*tx+sinT*ty + (double)hf;
			fy= -sinT*tx+cosT*ty + (double)hf;
			vv=linearInterpolate(mat,n,fx,fy);
			if(vv) {
				tmpx[i]=xx-hf;
				tmpy[i]=yy-hf;
				tmpv[i]=vv;
				tsum+=tmpv[i];
				i++;
			}
		}
	}
	free(mat);
	*x=tmpx;
	*y=tmpy;
	*val=tmpv;
	*sum=tsum;
	return i;
} 

void iPhonePhotoDarkenByGray(unsigned char* pSrc,unsigned int width,unsigned int height){
	int buf_size;
	
	unsigned char* p_temp;
	unsigned int minimum;
	p_temp=pSrc;
	buf_size=width*height;
	minimum=255;
	
	for(int i=0;i<buf_size;i++,p_temp+=4){
		if(minimum>*p_temp)minimum=*p_temp;
		if(minimum>*(p_temp+1))minimum=*(p_temp+1);
		if(minimum>*(p_temp+2))minimum=*(p_temp+2);
	}
		p_temp=pSrc;
	for(int i=0;i<buf_size;i++,p_temp+=4){
		*p_temp-=minimum;
		*(p_temp+1)-=minimum;
		*(p_temp+2)-=minimum;
	}
	p_temp=pSrc;
	for(int i=0;i<buf_size;i++,p_temp+=4){
		if(*p_temp<225)*p_temp/=2;
		if(*(p_temp+1)<225)*(p_temp+1)/=2;
		if(*(p_temp+2)<225)*(p_temp+2)/=2;
	}
	
}
void iPhonePhotoGrayFilter1(unsigned char* pSrc,unsigned int width,unsigned int height,unsigned int value){
	int buf_size;
	
	unsigned char* p_temp;
	unsigned int rgbSum;
	p_temp=pSrc;
	buf_size=width*height;
	for(int i=0;i<buf_size;i++,p_temp+=4){
		rgbSum=*p_temp+*(p_temp+1)+*(p_temp+2);
		rgbSum=rgbSum*value;
		rgbSum=rgbSum/50;
		if(rgbSum>765)rgbSum=765;
		*p_temp=rgbSum/3;
		*(p_temp+1)=*p_temp;
		*(p_temp+2)=*p_temp;
		
	}
	
}
void iPhonePhotoPaperFilter1(unsigned char* pSrc,unsigned int width,unsigned int height){
	int buf_size;
	
	unsigned char* p_temp;

	p_temp=pSrc;
	buf_size=width*height;
	for(int i=0;i<buf_size;i++,p_temp+=4){
		if(rand()%2){
			*p_temp=(*p_temp)*(*p_temp)/255;
			*(p_temp+1)=*(p_temp+1)* *(p_temp+1)/255;
			*(p_temp+2)=*(p_temp+2)* *(p_temp+2)/255;
		}
		
	}
	
}

void iPhonePhotoGrayFilter2(unsigned char* pSrc,unsigned int width,unsigned int height){
	int buf_size;
	
	unsigned char* p_temp;
	unsigned int gray;
	p_temp=pSrc;
	buf_size=width*height;
	for(int i=0;i<buf_size;i++,p_temp+=4){
		gray = ((2989*(*(p_temp)))+(5866*(*(p_temp+1)))+(1145*(*(p_temp+2))))/10000;
		*p_temp=gray/3;
		*(p_temp+1)=*p_temp;
		*(p_temp+2)=*p_temp;
		
	}
	
}

void iPhonePhotoGFilter1(unsigned char* pSrc,unsigned int width,unsigned int height){
	int buf_size;
	
	unsigned char* p_temp;

	p_temp=pSrc;
	buf_size=width*height;
	for(int i=0;i<buf_size;i++,p_temp+=4){
	//	rgbSum=*p_temp+*(p_temp+1)+*(p_temp+2);
		if(*(p_temp)<100)*(p_temp)*=2;
		else if(*(p_temp)<200)*(p_temp)=100+*(p_temp)/2;
		if(*(p_temp+1)<100)*(p_temp+1)*=2;
		else if(*(p_temp+1)<200)*(p_temp+1)=100+*(p_temp+1)/2;
		if(*(p_temp+2)<100)*(p_temp+2)*=2;
		else if(*(p_temp+2)<200)*(p_temp+2)=100+*(p_temp+2)/2;
		
	}
	//iPhonePhotoOverlayFilter(pSrc,width,height);
	
	
}
void iPhonePhotoSobelFilter(unsigned char* pSrc,unsigned int width,unsigned int height){
	unsigned int buf_size,line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;
	//[height+2][width*4+8];
	int sobelR,sobelG,sobelB;
	int cols_pixelR,cols_pixelG,cols_pixelB;
	int row_pixelR,row_pixelG,row_pixelB;
	
	
	
	line_size=width*4;
		buf_line_size=width*4+8;
	buf_size=height*line_size;
	pBuf=(unsigned char*)malloc((height+2)*buf_line_size);
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	for(int w=0; w<(width*4); w++)
	{
		//pBuf[0][w+4] =*(pTemp+w);
		*(pBuf+w+4)=*(pTemp+w);
	}
	
	
	for(int w=0; w<(width*4); w++)
	{
		//pBuf[height+1][w+4] =*(pTemp+w+(height-1)*line_size);
		*(pBuf+w+4+(height+1)*buf_line_size) =*(pTemp+w+(height-1)*line_size);
		
	}
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<4; w++)
		{
			//pBuf[h+1][w] = *(pTemp+w+h*line_size);
			*(pBuf+w+(h+1)*buf_line_size) =*(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<4; w++)
		{
			//	pBuf[h+1][w+4+width*4] =*(pTemp+(width-1)*4+w+h*line_size);
			*(pBuf+w+4+width*4+(h+1)*buf_line_size) =*(pTemp+(width-1)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//pBuf[h+1][w+4] = *(pTemp+w+h*line_size);
			*(pBuf+w+4+(h+1)*buf_line_size) =*(pTemp+w+h*line_size);
		}
	}
	
	
	pTemp=pSrc;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width); w++)
		{
 			row_pixelR = 0;
			row_pixelG = 0;
			row_pixelB = 0;
			// line - 1
			row_pixelR += *pBufTemp;
			row_pixelG += *(pBufTemp+1);
			row_pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	row_pixelR += 2*(*pBufTemp);
			row_pixelG += 2*(*(pBufTemp+1));
			row_pixelB += 2*(*(pBufTemp+2));
			pBufTemp += 4;
			row_pixelR += *pBufTemp;
			row_pixelG += *(pBufTemp+1);
			row_pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 2
			pBufTemp += 4;
			pBufTemp += 4;
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 3
			row_pixelR -= *pBufTemp;
			row_pixelG -= *(pBufTemp+1);
			row_pixelB -= *(pBufTemp+2);
		  	pBufTemp += 4;
		 	row_pixelR -= 2*(*pBufTemp);
			row_pixelG -= 2*(*(pBufTemp+1));
			row_pixelB -= 2*(*(pBufTemp+2));
			pBufTemp += 4;
			row_pixelR -= *pBufTemp;
			row_pixelG -= *(pBufTemp+1);
			row_pixelB -= *(pBufTemp+2);
			pBufTemp = pBufTemp - (buf_line_size << 1) - 8;
		
			cols_pixelR = 0;
			cols_pixelG = 0;
			cols_pixelB = 0;
			// line - 1
			cols_pixelR += *pBufTemp;
			cols_pixelG += *(pBufTemp+1);
			cols_pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
			pBufTemp += 4;
			cols_pixelR -= *pBufTemp;
			cols_pixelG -= *(pBufTemp+1);
			cols_pixelB -= *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 2
			cols_pixelR += 2*(*pBufTemp);
			cols_pixelG += 2*(*(pBufTemp+1));
			cols_pixelB += 2*(*(pBufTemp+2));
		  	pBufTemp += 4;
			pBufTemp += 4;
			cols_pixelR -= 2*(*pBufTemp);
			cols_pixelG -= 2*(*(pBufTemp+1));
			cols_pixelB -= 2*(*(pBufTemp+2));
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 3
			cols_pixelR += *pBufTemp;
			cols_pixelG += *(pBufTemp+1);
			cols_pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
			pBufTemp += 4;
			cols_pixelR -= *pBufTemp;
			cols_pixelG -= *(pBufTemp+1);
			cols_pixelB -= *(pBufTemp+2);
			pBufTemp = pBufTemp - (buf_line_size << 1) - 4;

			if(row_pixelR<0) row_pixelR *= (-1);
			if(row_pixelG<0) row_pixelG *= (-1);
			if(row_pixelB<0) row_pixelB *= (-1);
			if(cols_pixelR<0) cols_pixelR *= (-1);
			if(cols_pixelG<0) cols_pixelG *= (-1);
			if(cols_pixelB<0) cols_pixelB *= (-1);
			sobelR = (row_pixelR) + (cols_pixelR);
			sobelG = (row_pixelG) + (cols_pixelG);
			sobelB = (row_pixelB) + (cols_pixelB);
			//total += sobel;
			if(sobelR>50){
				sobelR=sobelR+25;
				sobelG=(sobelR-50)/2;
				sobelB=(sobelR-50)/2;	
			}
			else {
				sobelR=0;
				sobelG=0;
				sobelB=0;	
			}
			if(sobelR>255){
				sobelR=255;
				sobelG=205;
				sobelB=205;	
			}
			if(sobelR<0)sobelR=0;
			if(sobelG<0)sobelG=0;
			if(sobelB<0)sobelB=0;
			*(pTemp+0) = sobelR;
			*(pTemp+1) = sobelG;
			*(pTemp+2) = sobelB;
			pTemp += 4;
		}
		pBufTemp += 8;
	} 
	//iPhonePhotoToneFilter(pSrc, width, height, 255, 0, 0);
		return;
	
}
void iPhonePhotoGaussianFilter(unsigned char* pSrc,unsigned int width,unsigned int height){

	unsigned int buf_size,line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char pBuf[width*4][height];
	int pixelR,pixelG,pixelB;
	line_size=width*4;
	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	for(int i=0; i<buf_size; i++)
	{
		*(pBufTemp+i) = *(pTemp+i);
	}
	
	
	
	pTemp+=line_size+4;
	for(int h=1; h<(height-1); h++)
	{
		for(int w=1; w<(width-1); w++)
		{
 			pixelR = 0;
			pixelG = 0;
			pixelB = 0;
			// line - 1
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += 2*(*pBufTemp);
			pixelG += 2*(*(pBufTemp+1));
			pixelB += 2*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 8;
			// line - 2
			pixelR += 2*(*pBufTemp);
			pixelG += 2*(*(pBufTemp+1));
			pixelB += 2*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	pixelR += 4*(*pBufTemp);
			pixelG += 4*(*(pBufTemp+1));
			pixelB += 4*(*(pBufTemp+2));
			pBufTemp += 4;
		 	pixelR += 2*(*pBufTemp);
			pixelG += 2*(*(pBufTemp+1));
			pixelB += 2*(*(pBufTemp+2));
			pBufTemp = pBufTemp + line_size - 8;
			// line - 3
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += 2*(*pBufTemp);
			pixelG += 2*(*(pBufTemp+1));
			pixelB += 2*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp - (line_size << 1) - 4;
			
					
			if(pixelR<0) pixelR *= (-1);
			if(pixelG<0) pixelG *= (-1);
			if(pixelB<0) pixelB *= (-1);

			pixelR/=16;
			pixelG/=16;
			pixelB/=16;
			//total += sobel;
			
			*(pTemp+0) = pixelR;
			*(pTemp+1) = pixelG;
			*(pTemp+2) = pixelB;
			pTemp += 4;
		}
		pTemp +=8 ;
		pBufTemp += 8;
	} 
	// 2. Top
	pTemp = (unsigned char *)pSrc;
	pBufTemp = (unsigned char *)pBuf;
	for(int w=0; w<width; w++) 
	{
		*(pTemp+0) = *(pBufTemp+0);
		*(pTemp+1) = *(pBufTemp+1);
		*(pTemp+2) = *(pBufTemp+2);
		pTemp += 4;
		pBufTemp += 4;
	}
	// 3. Bottom
	pBufTemp = (unsigned char *)((unsigned long)pBuf + width * (height-1) * 4 );
	pTemp = (unsigned char *)((unsigned long)pSrc + width * (height-1) * 4 );
	for(int w=0; w<width; w++) 
	{
		*(pTemp+0) = *(pBufTemp+0);
		*(pTemp+1) = *(pBufTemp+1);
		*(pTemp+2) = *(pBufTemp+2);
		pTemp += 4;
		pBufTemp += 4;
	}
	// 4. Left
	pBufTemp = (unsigned char *)pBuf;
	pTemp = (unsigned char *)pSrc;
	for(int h=0; h<height; h++)
	{
		*(pTemp+0) = *(pBufTemp+0);
		*(pTemp+1) = *(pBufTemp+1);
		*(pTemp+2) = *(pBufTemp+2);
		pTemp = pTemp + line_size;
		pBufTemp = pBufTemp + line_size;
	}
	// 5. Right
	pBufTemp = (unsigned char *)((unsigned long)pBuf + (width-1) * 4 );
	pTemp = (unsigned char *)((unsigned long)pSrc + (width-1) * 4 );
	for(int h=0; h<height; h++) 
	{
		*(pTemp+0) = *(pBufTemp+0);
		*(pTemp+1) = *(pBufTemp+1);
		*(pTemp+2) = *(pBufTemp+2);
		pTemp = pTemp + line_size;
		pBufTemp = pBufTemp + line_size;
	}
	
	return;
	
}

void iPhonePhotoMeanFilter(unsigned char* pSrc,unsigned int width,unsigned int height){
	
	unsigned int buf_size,line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;
	int pixelR,pixelG,pixelB;
	line_size=width*4;
	buf_line_size=width*4+8;
	buf_size=height*line_size;
	pTemp=pSrc;
	pBuf=(unsigned char*)malloc((height+2)*buf_line_size);
	pBufTemp=(unsigned char*) pBuf;
	
	for(int w=0; w<(width*4); w++)
	{
		//pBuf[0][w+4] =*(pTemp+w);
		*(pBuf+w+4)=*(pTemp+w);
	}
	
	
	for(int w=0; w<(width*4); w++)
	{
		//pBuf[height+1][w+4] =*(pTemp+w+(height-1)*line_size);
		*(pBuf+w+4+(height+1)*buf_line_size) =*(pTemp+w+(height-1)*line_size);

	}
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<4; w++)
		{
			//pBuf[h+1][w] = *(pTemp+w+h*line_size);
			*(pBuf+w+(h+1)*buf_line_size) =*(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<4; w++)
		{
		//	pBuf[h+1][w+4+width*4] =*(pTemp+(width-1)*4+w+h*line_size);
			*(pBuf+w+4+width*4+(h+1)*buf_line_size) =*(pTemp+(width-1)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//pBuf[h+1][w+4] = *(pTemp+w+h*line_size);
			*(pBuf+w+4+(h+1)*buf_line_size) =*(pTemp+w+h*line_size);
		}
	}
	
	
	
	
	

	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width); w++)
		{
 			pixelR = 0;
			pixelG = 0;
			pixelB = 0;
			// line - 1
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 2
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 3
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp - (buf_line_size << 1) - 4;
			
			
			if(pixelR<0) pixelR *= (-1);
			if(pixelG<0) pixelG *= (-1);
			if(pixelB<0) pixelB *= (-1);
			
			pixelR/=9;
			pixelG/=9;
			pixelB/=9;
			//total += sobel;
			
			*(pTemp+0) = pixelR;
			*(pTemp+1) = pixelG;
			*(pTemp+2) = pixelB;
			pTemp += 4;
		}
		pBufTemp += 8;
	} 
	
	
	return;
	
}

void iPhonePhotoMedianFilter(unsigned char* pSrc,unsigned int width,unsigned int height){
	int l,k;
	unsigned int buf_size,line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char pBuf[width*4][height];
	unsigned char pixelR[9],pixelG[9],pixelB[9],temp;
	line_size=width*4;
	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	for(int i=0; i<buf_size; i++)
	{
		*(pBufTemp+i) = *(pTemp+i);
	}
	
	
	
	pTemp+=line_size+4;
	for(int h=1; h<(height-1); h++)
	{
		for(int w=1; w<(width-1); w++)
		{
			// line - 1
			pixelR[0] = *pBufTemp;
			pixelG[0] = *(pBufTemp+1);
			pixelB[0] += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[1] = *pBufTemp;
			pixelG[1] = *(pBufTemp+1);
			pixelB[1] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[2] = *pBufTemp;
			pixelG[2] = *(pBufTemp+1);
			pixelB[2] = *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 8;
			// line - 2
			pixelR[3] = *pBufTemp;
			pixelG[3] = *(pBufTemp+1);
			pixelB[3] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[4] = *pBufTemp;
			pixelG[4] = *(pBufTemp+1);
			pixelB[4] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[5] = *pBufTemp;
			pixelG[5] = *(pBufTemp+1);
			pixelB[5] = *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 8;
			// line - 3
			pixelR[6] = *pBufTemp;
			pixelG[6] = *(pBufTemp+1);
			pixelB[6] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[7] = *pBufTemp;
			pixelG[7] = *(pBufTemp+1);
			pixelB[7] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[8] = *pBufTemp;
			pixelG[8] = *(pBufTemp+1);
			pixelB[8] = *(pBufTemp+2);
			pBufTemp = pBufTemp - (line_size << 1) - 4;
			
			
			for(k=0;k<9;k++){
				for(l=0;l<9;l++){
					if(pixelR[l] < pixelR[l+1]) {
                        temp = pixelR[l];
                        pixelR[l] = pixelR[l+1];
                        pixelR[l+1] = temp;
                    }
                }
            }
			
			for(k=0;k<9;k++){
				for(l=0;l<9;l++){
					if(pixelG[l] < pixelG[l+1]) {
                        temp = pixelG[l];
                        pixelG[l] = pixelG[l+1];
                        pixelG[l+1] = temp;
                    }
                }
            }
			
			for(k=0;k<9;k++){
				for(l=0;l<9;l++){
					if(pixelB[l] < pixelB[l+1]) {
                        temp = pixelB[l];
                        pixelB[l] = pixelB[l+1];
                        pixelB[l+1] = temp;
                    }
                }
            }
			
		
	

			//total += sobel;
			
			*(pTemp+0) = pixelR[5];
			*(pTemp+1) = pixelG[5];
			*(pTemp+2) = pixelB[5];
			pTemp += 4;
		}
		pTemp +=8 ;
		pBufTemp += 8;
	} 
	// 2. Top
	pTemp = (unsigned char *)pSrc;
	pBufTemp = (unsigned char *)pBuf;
	for(int w=0; w<width; w++) 
	{
		*(pTemp+0) = *(pBufTemp+0);
		*(pTemp+1) = *(pBufTemp+1);
		*(pTemp+2) = *(pBufTemp+2);
		pTemp += 4;
		pBufTemp += 4;
	}
	// 3. Bottom
	pBufTemp = (unsigned char *)((unsigned long)pBuf + width * (height-1) * 4 );
	pTemp = (unsigned char *)((unsigned long)pSrc + width * (height-1) * 4 );
	for(int w=0; w<width; w++) 
	{
		*(pTemp+0) = *(pBufTemp+0);
		*(pTemp+1) = *(pBufTemp+1);
		*(pTemp+2) = *(pBufTemp+2);
		pTemp += 4;
		pBufTemp += 4;
	}
	// 4. Left
	pBufTemp = (unsigned char *)pBuf;
	pTemp = (unsigned char *)pSrc;
	for(int h=0; h<height; h++)
	{
		*(pTemp+0) = *(pBufTemp+0);
		*(pTemp+1) = *(pBufTemp+1);
		*(pTemp+2) = *(pBufTemp+2);
		pTemp = pTemp + line_size;
		pBufTemp = pBufTemp + line_size;
	}
	// 5. Right
	pBufTemp = (unsigned char *)((unsigned long)pBuf + (width-1) * 4 );
	pTemp = (unsigned char *)((unsigned long)pSrc + (width-1) * 4 );
	for(int h=0; h<height; h++) 
	{
		*(pTemp+0) = *(pBufTemp+0);
		*(pTemp+1) = *(pBufTemp+1);
		*(pTemp+2) = *(pBufTemp+2);
		pTemp = pTemp + line_size;
		pBufTemp = pBufTemp + line_size;
	}
	
	return;
	
}


void iPhonePhotoToneFilter(unsigned char* pSrc,unsigned int width,unsigned int height,unsigned char R,unsigned char G,unsigned char B){
	int buf_size;
	unsigned char* p_temp;
	unsigned int rgbSum;
	p_temp=pSrc;
	buf_size=width*height;
	for(int i=0;i<buf_size;i++,p_temp+=4){
		rgbSum=*p_temp+*(p_temp+1)+*(p_temp+2);
		*p_temp=(rgbSum/3*R)/255;
		*(p_temp+1)=(rgbSum/3*G)/255;
		*(p_temp+2)=(rgbSum/3*B)/255;
		
	}
	
}


void iPhonePhotoTestFilter(unsigned char* pSrc,unsigned int width,unsigned int height){
	int buf_size;
	unsigned char* p_temp;

	p_temp=pSrc;
	buf_size=width*height;
	
	for(int i=0;i<buf_size;i++,p_temp+=4)*(p_temp+3)=255;

	 
	
}

void iPhonePhotoSpecialGaussianFilter(unsigned char* pSrc,unsigned int width,unsigned int height){
	unsigned int buf_size,line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char pBuf[width*4][height];
	int pixelR,pixelG,pixelB;
	line_size=width*4;
	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	for(int i=0; i<buf_size; i++)
	{
		*(pBufTemp+i) = *(pTemp+i);
	}
	
	
	
	pTemp+=line_size*4+4*4;
	for(int h=4; h<(height-4); h++)
	{
		for(int w=4; w<(width-4); w++)
		{
 			pixelR = 0;
			pixelG = 0;
			pixelB = 0;
			// line - 1
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 32;
			// line - 2
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 32;
			// line - 3
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 32;
			//line -4
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 32;
			// line - 5
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp*20;
			pixelG += *(pBufTemp+1)*20;
			pixelB += *(pBufTemp+2)*20;
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 32;
			//line - 6
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 32;
			
			//line - 7
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 32;
			//line -8
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 32;
			//line - 9
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		

			
			
			pBufTemp = pBufTemp - (line_size << 3) - 4*7;
			
			
			if(pixelR<0) pixelR *= (-1);
			if(pixelG<0) pixelG *= (-1);
			if(pixelB<0) pixelB *= (-1);
			
			pixelR/=100;
			pixelG/=100;
			pixelB/=100;
			//total += sobel;
			
			*(pTemp+0) = pixelR;
			*(pTemp+1) = pixelG;
			*(pTemp+2) = pixelB;
			pTemp += 4;
		}
		pTemp +=4*8;
		pBufTemp += 4*8;
	} 
	// 2. Top
	pTemp = (unsigned char *)pSrc;
	pBufTemp = (unsigned char *)pBuf;
	for(int w=0; w<width; w++) 
	{
		*(pTemp+0) = *(pBufTemp+0);
		*(pTemp+1) = *(pBufTemp+1);
		*(pTemp+2) = *(pBufTemp+2);
		pTemp += 4;
		pBufTemp += 4;
	}
	// 3. Bottom
	pBufTemp = (unsigned char *)((unsigned long)pBuf + width * (height-1) * 4 );
	pTemp = (unsigned char *)((unsigned long)pSrc + width * (height-1) * 4 );
	for(int w=0; w<width; w++) 
	{
		*(pTemp+0) = *(pBufTemp+0);
		*(pTemp+1) = *(pBufTemp+1);
		*(pTemp+2) = *(pBufTemp+2);
		pTemp += 4;
		pBufTemp += 4;
	}
	// 4. Left
	pBufTemp = (unsigned char *)pBuf;
	pTemp = (unsigned char *)pSrc;
	for(int h=0; h<height; h++)
	{
		*(pTemp+0) = *(pBufTemp+0);
		*(pTemp+1) = *(pBufTemp+1);
		*(pTemp+2) = *(pBufTemp+2);
		pTemp = pTemp + line_size;
		pBufTemp = pBufTemp + line_size;
	}
	// 5. Right
	pBufTemp = (unsigned char *)((unsigned long)pBuf + (width-1) * 4 );
	pTemp = (unsigned char *)((unsigned long)pSrc + (width-1) * 4 );
	for(int h=0; h<height; h++) 
	{
		*(pTemp+0) = *(pBufTemp+0);
		*(pTemp+1) = *(pBufTemp+1);
		*(pTemp+2) = *(pBufTemp+2);
		pTemp = pTemp + line_size;
		pBufTemp = pBufTemp + line_size;
	}
	
	return;
	
	

}

void iPhonePhotoMeanFilter5x5(unsigned char* pSrc,unsigned int width,unsigned int height){
	
	unsigned int buf_size,line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char pBuf[width*4][height];
	int pixelR,pixelG,pixelB;
	line_size=width*4;
	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	for(int i=0; i<buf_size; i++)
	{
		*(pBufTemp+i) = *(pTemp+i);
	}
	
	
	
	pTemp+=line_size*2+8;
	for(int h=2; h<(height-2); h++)
	{
		for(int w=2; w<(width-2); w++)
		{
 			pixelR = 0;
			pixelG = 0;
			pixelB = 0;
			// line - 1
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 16;
			// line - 2
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 16;
			// line - 3
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 16;
			// line - 4
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 16;
			// line - 5
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			
			
			pBufTemp = pBufTemp - (line_size << 2) - 12;
			
			
			if(pixelR<0) pixelR *= (-1);
			if(pixelG<0) pixelG *= (-1);
			if(pixelB<0) pixelB *= (-1);
			
			pixelR/=25;
			pixelG/=25;
			pixelB/=25;
			//total += sobel;
			
			*(pTemp+0) = pixelR;
			*(pTemp+1) = pixelG;
			*(pTemp+2) = pixelB;
			pTemp += 4;
		}
		pTemp +=16 ;
		pBufTemp += 16;
	} 
	// 2. Top
	/*
	pTemp = (unsigned char *)pSrc;
	pBufTemp = (unsigned char *)pBuf;
	for(int w=0; w<width; w++) 
	{
		*(pTemp+0) = *(pBufTemp+0);
		*(pTemp+1) = *(pBufTemp+1);
		*(pTemp+2) = *(pBufTemp+2);
		pTemp += 4;
		pBufTemp += 4;
	}
	 */
	// botton
	pTemp = (unsigned char *)pSrc;
	pBufTemp = (unsigned char *)pBuf;
	pTemp+=buf_size+8-line_size;
	pBufTemp += buf_size-line_size*3;
	for(int w=2; w<(width-2); w++) 
	{
		pixelR = 0;	pixelG = 0;
		pixelB = 0;
		// line - 1
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 16;
		// line - 2
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 16;
		// line - 3
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		
		pBufTemp = pBufTemp - (line_size << 1) - 12;
		
		
		if(pixelR<0) pixelR *= (-1);
		if(pixelG<0) pixelG *= (-1);
		if(pixelB<0) pixelB *= (-1);
		
		pixelR/=15;
		pixelG/=15;
		pixelB/=15;
		//total += sobel;
		
		*(pTemp+0) = pixelR;
		*(pTemp+1) = pixelG;
		*(pTemp+2) = pixelB;
		pTemp += 4;
	
	}
	
	//botton line 2
	// botton
	pTemp = (unsigned char *)pSrc;
	pBufTemp = (unsigned char *)pBuf;
	pTemp+=buf_size+8-line_size*2;
	pBufTemp += buf_size-line_size*4;
	for(int w=2; w<(width-2); w++) 
	{
		pixelR = 0;
		pixelG = 0;
		pixelB = 0;
		// line - 1
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 16;
		// line - 2
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 16;
		// line - 3
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 16;
		//line - 4
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp - line_size*3  - 12;
		
		
		if(pixelR<0) pixelR *= (-1);
		if(pixelG<0) pixelG *= (-1);
		if(pixelB<0) pixelB *= (-1);
		
		pixelR/=20;
		pixelG/=20;
		pixelB/=20;
		//total += sobel;
		
		*(pTemp+0) = pixelR;
		*(pTemp+1) = pixelG;
		*(pTemp+2) = pixelB;
		pTemp += 4;
		
	}
	// 3. Bottom
	/*
	pBufTemp = (unsigned char *)((unsigned long)pBuf + width * (height-1) * 4 );
	pTemp = (unsigned char *)((unsigned long)pSrc + width * (height-1) * 4 );
	for(int w=0; w<width; w++) 
	{
		*(pTemp+0) = *(pBufTemp+0);
		*(pTemp+1) = *(pBufTemp+1);
		*(pTemp+2) = *(pBufTemp+2);
		pTemp += 4;
		pBufTemp += 4;
	}
	*/
	pTemp = (unsigned char *)pSrc;
	pBufTemp = (unsigned char *)pBuf;
	pTemp+=8;
	for(int w=2; w<(width-2); w++) 
	{
		pixelR = 0;
		pixelG = 0;
		pixelB = 0;
		// line - 1
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 16;
		// line - 2
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 16;
		// line - 3
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		
		pBufTemp = pBufTemp - (line_size << 1) - 12;
		
		
		if(pixelR<0) pixelR *= (-1);
		if(pixelG<0) pixelG *= (-1);
		if(pixelB<0) pixelB *= (-1);
		
		pixelR/=15;
		pixelG/=15;
		pixelB/=15;
		//total += sobel;
		
		*(pTemp+0) = pixelR;
		*(pTemp+1) = pixelG;
		*(pTemp+2) = pixelB;
		pTemp += 4;
		
	}
	// top line -2
	pTemp = (unsigned char *)pSrc;
	pBufTemp = (unsigned char *)pBuf;
	pTemp+=line_size+ 8;
	for(int w=2; w<(width-2); w++) 
	{
		pixelR = 0;
		pixelG = 0;
		pixelB = 0;
		// line - 1
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 16;
		// line - 2
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 16;
		//line -3
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 16;
		// line - 4
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		
		pBufTemp = pBufTemp - line_size*3  - 12;
		
		
		if(pixelR<0) pixelR *= (-1);
		if(pixelG<0) pixelG *= (-1);
		if(pixelB<0) pixelB *= (-1);
		
		pixelR/=20;
		pixelG/=20;
		pixelB/=20;
		//total += sobel;
		
		*(pTemp+0) = pixelR;
		*(pTemp+1) = pixelG;
		*(pTemp+2) = pixelB;
		pTemp += 4;
		
	}
	
	
	// 4. Left
	pBufTemp = (unsigned char *)pBuf;
	pTemp = (unsigned char *)pSrc;
	pTemp+=line_size*2;
	for(int h=2; h<(height-2); h++)
	{
		pixelR = 0;
		pixelG = 0;
		pixelB = 0;
		// line - 1
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 8;
		// line - 2
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 8;
		// line - 3
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 8;
		// line - 4
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 8;
		// line - 5
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		
		pBufTemp = pBufTemp - line_size*3 - 8;
		
		
		if(pixelR<0) pixelR *= (-1);
		if(pixelG<0) pixelG *= (-1);
		if(pixelB<0) pixelB *= (-1);
		
		pixelR/=15;
		pixelG/=15;
		pixelB/=15;
		//total += sobel;
		
		*(pTemp+0) = pixelR;
		*(pTemp+1) = pixelG;
		*(pTemp+2) = pixelB;
		pTemp += line_size;
	}
	
	pBufTemp = (unsigned char *)pBuf;
	pTemp = (unsigned char *)pSrc;
	pTemp+=line_size*2+4;
	for(int h=2; h<(height-2); h++)
	{
		pixelR = 0;
		pixelG = 0;
		pixelB = 0;
		// line - 1
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 12;
		// line - 2
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 12;
		// line - 3
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 12;
		// line - 4
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 12;
		// line - 5
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		
		pBufTemp = pBufTemp - line_size*3 - 12;
		
		
		if(pixelR<0) pixelR *= (-1);
		if(pixelG<0) pixelG *= (-1);
		if(pixelB<0) pixelB *= (-1);
		
		pixelR/=20;
		pixelG/=20;
		pixelB/=20;
		//total += sobel;
		
		*(pTemp+0) = pixelR;
		*(pTemp+1) = pixelG;
		*(pTemp+2) = pixelB;
		pTemp += line_size;
	}
	
	// 5. Right
	/*
	pBufTemp = (unsigned char *)((unsigned long)pBuf + (width-1) * 4 );
	pTemp = (unsigned char *)((unsigned long)pSrc + (width-1) * 4 );
	for(int h=0; h<height; h++) 
	{
		*(pTemp+0) = *(pBufTemp+0);
		*(pTemp+1) = *(pBufTemp+1);
		*(pTemp+2) = *(pBufTemp+2);
		pTemp = pTemp + line_size;
		pBufTemp = pBufTemp + line_size;
	}*/
	pBufTemp = (unsigned char *)pBuf;
	pTemp = (unsigned char *)pSrc;
	pTemp+=line_size*3-4;
	pBufTemp+=line_size-12;
	for(int h=2; h<(height-2); h++)
	{
		pixelR = 0;
		pixelG = 0;
		pixelB = 0;
		// line - 1
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 8;
		// line - 2
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 8;
		// line - 3
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 8;
		// line - 4
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 8;
		// line - 5
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		
		pBufTemp = pBufTemp - line_size*3 - 8;
		
		
		if(pixelR<0) pixelR *= (-1);
		if(pixelG<0) pixelG *= (-1);
		if(pixelB<0) pixelB *= (-1);
		
		pixelR/=15;
		pixelG/=15;
		pixelB/=15;
		//total += sobel;
		
		*(pTemp+0) = pixelR;
		*(pTemp+1) = pixelG;
		*(pTemp+2) = pixelB;
		pTemp += line_size;
	}
	
	pBufTemp = (unsigned char *)pBuf;
	pTemp = (unsigned char *)pSrc;
	pTemp+=line_size*3-8;
	pBufTemp+=line_size-16;
	for(int h=2; h<(height-2); h++)
	{
		pixelR = 0;
		pixelG = 0;
		pixelB = 0;
		// line - 1
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 12;
		// line - 2
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 12;
		// line - 3
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 12;
		// line - 4
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp = pBufTemp + line_size - 12;
		// line - 5
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		pBufTemp += 4;
		pixelR += *pBufTemp;
		pixelG += *(pBufTemp+1);
		pixelB += *(pBufTemp+2);
		
		pBufTemp = pBufTemp - line_size*3 - 12;
		
		
		if(pixelR<0) pixelR *= (-1);
		if(pixelG<0) pixelG *= (-1);
		if(pixelB<0) pixelB *= (-1);
		
		pixelR/=20;
		pixelG/=20;
		pixelB/=20;
		//total += sobel;
		
		*(pTemp+0) = pixelR;
		*(pTemp+1) = pixelG;
		*(pTemp+2) = pixelB;
		pTemp += line_size;
	}
	
	//0.0
	pBufTemp = (unsigned char *)pBuf;
	pTemp = (unsigned char *)pSrc;
	pixelR = 0;
	pixelG = 0;
	pixelB = 0;
	// line - 1
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp = pBufTemp + line_size - 8;
	// line - 2
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp = pBufTemp + line_size - 8;
	// line - 3
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	
	
	if(pixelR<0) pixelR *= (-1);
	if(pixelG<0) pixelG *= (-1);
	if(pixelB<0) pixelB *= (-1);
	
	pixelR/=9;
	pixelG/=9;
	pixelB/=9;
	//total += sobel;
	
	*(pTemp+0) = pixelR;
	*(pTemp+1) = pixelG;
	*(pTemp+2) = pixelB;

	//0.1
	pBufTemp = (unsigned char *)pBuf;
	pTemp = (unsigned char *)pSrc;
	pTemp+=4;
	pixelR = 0;
	pixelG = 0;
	pixelB = 0;
	// line - 1
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp = pBufTemp + line_size - 12;
	// line - 2
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp = pBufTemp + line_size - 12;
	// line - 3
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);


	
	
	if(pixelR<0) pixelR *= (-1);
	if(pixelG<0) pixelG *= (-1);
	if(pixelB<0) pixelB *= (-1);
	
	pixelR/=12;
	pixelG/=12;
	pixelB/=12;
	//total += sobel;
	
	*(pTemp+0) = pixelR;
	*(pTemp+1) = pixelG;
	*(pTemp+2) = pixelB;
	
	//1.0
	pBufTemp = (unsigned char *)pBuf;
	pTemp = (unsigned char *)pSrc;
	pTemp+=line_size;
	pixelR = 0;
	pixelG = 0;
	pixelB = 0;
	// line - 1
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp = pBufTemp + line_size - 8;
	// line - 2
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp = pBufTemp + line_size - 8;
	// line - 3
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp = pBufTemp + line_size - 8;
	// line - 4
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	
	if(pixelR<0) pixelR *= (-1);
	if(pixelG<0) pixelG *= (-1);
	if(pixelB<0) pixelB *= (-1);
	
	pixelR/=12;
	pixelG/=12;
	pixelB/=12;
	//total += sobel;
	
	*(pTemp+0) = pixelR;
	*(pTemp+1) = pixelG;
	*(pTemp+2) = pixelB;

	//1.1
	pBufTemp = (unsigned char *)pBuf;
	pTemp = (unsigned char *)pSrc;
	pTemp+=line_size;
	pixelR = 0;
	pixelG = 0;
	pixelB = 0;
	// line - 1
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp = pBufTemp + line_size - 12;
	// line - 2
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp = pBufTemp + line_size - 12;
	// line - 3
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp = pBufTemp + line_size - 12;
	// line - 4
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	pBufTemp += 4;
	pixelR += *pBufTemp;
	pixelG += *(pBufTemp+1);
	pixelB += *(pBufTemp+2);
	
	if(pixelR<0) pixelR *= (-1);
	if(pixelG<0) pixelG *= (-1);
	if(pixelB<0) pixelB *= (-1);
	
	pixelR/=16;
	pixelG/=16;
	pixelB/=16;
	//total += sobel;
	
	*(pTemp+0) = pixelR;
	*(pTemp+1) = pixelG;
	*(pTemp+2) = pixelB;
	////////
	
	return;
	
}


void iPhonePhotoMeanFilter5x5_x(unsigned char* pSrc,unsigned int width,unsigned int height){
	
	unsigned int line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char pBuf[height+4][width*4+16];
	int pixelR,pixelG,pixelB;
	line_size=width*4;
	buf_line_size=width*4+16;
//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			pBuf[h][w+8] = *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			pBuf[h+height+2][w+8] = *(pTemp+w+(height-2+h)*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			pBuf[h+2][w] = *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			pBuf[h+2][w+8+width*4] = *(pTemp+(width-2)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
		pBuf[h+2][w+8] = *(pTemp+w+h*line_size);
		}
	}
	

	//pTemp+=line_size*2+8;
	for(int h=0; h<height; h++)
	{
		for(int w=0; w<width; w++)
		{
			
 			pixelR = 0;
			pixelG = 0;
			pixelB = 0;
			// line - 1
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 2
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 3
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 4
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 5
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			
			
			
			pBufTemp = pBufTemp - (buf_line_size << 2) - 12;
			
			
			if(pixelR<0) pixelR *= (-1);
			if(pixelG<0) pixelG *= (-1);
			if(pixelB<0) pixelB *= (-1);
			
			pixelR/=25;
			pixelG/=25;
			pixelB/=25;
			//total += sobel;
			
			*(pTemp+0) = pixelR;
			*(pTemp+1) = pixelG;
			*(pTemp+2) = pixelB;
			pTemp += 4;
		}
		//pTemp +=16 ;
		pBufTemp += 16;
	} 
	return;
	
}
void iPhonePhotoGaussianFilter5x5_x(unsigned char* pSrc,unsigned int width,unsigned int height){
	
	unsigned int line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;//[height+4][width*4+16];
	int pixelR,pixelG,pixelB;
	line_size=width*4;
	buf_line_size=width*4+16;
	pBuf=(unsigned char*)malloc(buf_line_size*(height+4));
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//			pBuf[h][w+8]
			*(pBuf+h*buf_line_size+w+8)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//			pBuf[h+height+2][w+8]
			*(pBuf+(h+height+2)*buf_line_size+w+8)= *(pTemp+w+(height-2+h)*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			//pBuf[h+2][w] 
			*(pBuf+(h+2)*buf_line_size+w)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			//	pBuf[h+2][w+8+width*4]
			*(pBuf+(h+2)*buf_line_size+w+8+width*4)= *(pTemp+(width-2)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//pBuf[h+2][w+8] 
			*(pBuf+(h+2)*buf_line_size+w+8)= *(pTemp+w+h*line_size);
		}
	}
	
	
	//pTemp+=line_size*2+8;
	for(int h=0; h<height; h++)
	{
		for(int w=0; w<width; w++)
		{
			
 			pixelR = 0;
			pixelG = 0;
			pixelB = 0;
			// line - 1
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += 4*(*pBufTemp);
			pixelG += 4*(*(pBufTemp+1));
			pixelB += 4*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 7*(*pBufTemp);
			pixelG += 7*(*(pBufTemp+1));
			pixelB += 7*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 4*(*pBufTemp);
			pixelG += 4*(*(pBufTemp+1));
			pixelB += 4*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 2
			pixelR += 4*(*pBufTemp);
			pixelG += 4*(*(pBufTemp+1));
			pixelB += 4*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	pixelR += 16*(*pBufTemp);
			pixelG += 16*(*(pBufTemp+1));
			pixelB += 16*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 26*(*pBufTemp);
			pixelG += 26*(*(pBufTemp+1));
			pixelB += 26*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 16*(*pBufTemp);
			pixelG += 16*(*(pBufTemp+1));
			pixelB += 16*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 4*(*pBufTemp);
			pixelG += 4*(*(pBufTemp+1));
			pixelB += 4*(*(pBufTemp+2));
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 3
			pixelR += 7*(*pBufTemp);
			pixelG += 7*(*(pBufTemp+1));
			pixelB += 7*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	pixelR += 26*(*pBufTemp);
			pixelG += 26*(*(pBufTemp+1));
			pixelB += 26*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 41*(*pBufTemp);
			pixelG += 41*(*(pBufTemp+1));
			pixelB += 41*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 26*(*pBufTemp);
			pixelG += 26*(*(pBufTemp+1));
			pixelB += 26*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 7*(*pBufTemp);
			pixelG += 7*(*(pBufTemp+1));
			pixelB += 7*(*(pBufTemp+2));
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 4
			pixelR += 4*(*pBufTemp);
			pixelG += 4*(*(pBufTemp+1));
			pixelB += 4*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	pixelR += 16*(*pBufTemp);
			pixelG += 16*(*(pBufTemp+1));
			pixelB += 16*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 26*(*pBufTemp);
			pixelG += 26*(*(pBufTemp+1));
			pixelB += 26*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 16*(*pBufTemp);
			pixelG += 16*(*(pBufTemp+1));
			pixelB += 16*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 4*(*pBufTemp);
			pixelG += 4*(*(pBufTemp+1));
			pixelB += 4*(*(pBufTemp+2));
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 5
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += 4*(*pBufTemp);
			pixelG += 4*(*(pBufTemp+1));
			pixelB += 4*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 7*(*pBufTemp);
			pixelG += 7*(*(pBufTemp+1));
			pixelB += 7*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 4*(*pBufTemp);
			pixelG += 4*(*(pBufTemp+1));
			pixelB += 4*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			
			
			
			pBufTemp = pBufTemp - (buf_line_size << 2) - 12;
			
			
			if(pixelR<0) pixelR *= (-1);
			if(pixelG<0) pixelG *= (-1);
			if(pixelB<0) pixelB *= (-1);
			
			pixelR/=273;
			pixelG/=273;
			pixelB/=273;
			//total += sobel;
			
			*(pTemp+0) = pixelR;
			*(pTemp+1) = pixelG;
			*(pTemp+2) = pixelB;
			pTemp += 4;
		}
		//pTemp +=16 ;
		pBufTemp += 16;
	}
	if(pBuf!=NULL)free(pBuf);
	return;
	
}

void iPhonePhotoBrightFilter(unsigned char* pSrc,unsigned int width,unsigned int height){
	int buf_size;
	int x;
	x=130;
	unsigned char* p_temp;
	p_temp=pSrc;
	buf_size=width*height;

	for(int i=0;i<buf_size;i++,p_temp+=4){

	
		if((*p_temp)*x/100<255) *p_temp=(*p_temp)*x/100;
		else *p_temp=255;
		if((*(p_temp+1))*x/100<225)*(p_temp+1)=(*(p_temp+1))*x/100;
		else *(p_temp+1)=255;
		if((*(p_temp+2))*x/100<225)*(p_temp+2)=(*(p_temp+2))*x/100;
		else *(p_temp+2)=255;
		
	

		
	}
	 
	
}
void iPhonePhotoContastFilter1(unsigned char* pSrc,unsigned int width,unsigned int height){
	int buf_size;
	unsigned char* p_temp;
	unsigned int pixelR,pixelG,pixelB,x;
	p_temp=pSrc;
	x=20;
	buf_size=width*height;
	pixelR=0;
	pixelG=0;
	pixelB=0;
	for(int i=0;i<buf_size;i++,p_temp+=4){
		pixelR+=*p_temp;
		pixelG+=*(p_temp+1);
		pixelB+=*(p_temp+2);
	}
	pixelR/=buf_size;
	pixelG/=buf_size;
	pixelB/=buf_size;
	p_temp=pSrc;
	for(int i=0;i<buf_size;i++,p_temp+=4){
		if(*p_temp>pixelR){
			if((*p_temp+x)<255){
				*p_temp+=x;
			}
			
			else{
				*p_temp=255;
			}
		}
		else{
			if((*p_temp)>x){
				*p_temp-=x;
				
			}
			else {
				*p_temp=0;
			}
			
		}
		
		if(*(p_temp+1)>pixelG){
			if((*(p_temp+1)+x)<255){
				*(p_temp+1)+=x;
			}
			else{
				*(p_temp+1)=255;
			}
		}
		else{
			if(*(p_temp+1)>x){
				*(p_temp+1)-=x;
			}
			else {
				
				*(p_temp+1)=0;
			}
			
		}		
		
		if(*(p_temp+2)>pixelB){
			if((*(p_temp+2)+x)<255){
				*(p_temp+2)+=x;
			}
			else {
				*(p_temp+2)=255;
			}
		}
		else{
			if(*(p_temp+2)>x){
				*(p_temp+2)-=x;
			}
			else{
				*(p_temp+2)=0;
			}
		}
	}
}

void iPhonePhotoContastFilter(unsigned char* pSrc,unsigned int width,unsigned int height){
	int buf_size;
	unsigned char* p_temp;
	unsigned int rgbSum;
	p_temp=pSrc;
	buf_size=width*height;
	
	for(int i=0;i<buf_size;i++,p_temp+=4){
		rgbSum=*p_temp+*(p_temp+1)+*(p_temp+2);
		rgbSum*=100;
		
		
		if((*p_temp)*rgbSum/50000<255) *p_temp=(*p_temp)*rgbSum/50000;
		else *p_temp=255;
		if((*(p_temp+1))*rgbSum/50000<225)*(p_temp+1)=(*(p_temp+1))*rgbSum/50000;
		else *(p_temp+1)=255;
		if((*(p_temp+2))*rgbSum/50000<225)*(p_temp+2)=(*(p_temp+2))*rgbSum/50000;
		else *(p_temp+2)=255;
		
		
		
		
	}
	
	
}


void iPhonePhotoOliFilter(unsigned char* pSrc,unsigned int width,unsigned int height){
	
	unsigned int line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char pBuf[height+4][width*4+16];
	int pixelR,pixelG,pixelB;
	int random;
	line_size=width*4;
	buf_line_size=width*4+16;
	
	
	
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			pBuf[h][w+8] = *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			pBuf[h+height+2][w+8] = *(pTemp+w+(height-2+h)*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			pBuf[h+2][w] = *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			pBuf[h+2][w+8+width*4] = *(pTemp+(width-2)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			pBuf[h+2][w+8] = *(pTemp+w+h*line_size);
		}
	}
	
	
	//pTemp+=line_size*2+8;
	for(int h=0; h<height; h++)
	{
		for(int w=0; w<width; w++)
		{
			
			random=rand()%25;
			
 			pixelR = 0;
			pixelG = 0;
			pixelB = 0;
			// line - 1
			if(random==0){
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	
			}
			pBufTemp += 4;
			 if(random==1){
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			}
			pBufTemp += 4;
			 if(random==2){
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			}
			pBufTemp += 4;
			 if(random==3){
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			}
			pBufTemp += 4;
			 if(random==4){
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			}
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 2
			 if(random==5){
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			}
		  	pBufTemp += 4;
			 if(random==6){
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			}
			pBufTemp += 4;
			 if(random==7){
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			}
			pBufTemp += 4;
			 if(random==8){
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			}
			pBufTemp += 4;
			 if(random==9){
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			}
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 3
			 if(random==10){
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			}
		  	pBufTemp += 4;
			 if(random==11){
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			}
			pBufTemp += 4;
			 if(random==12){
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			}
			pBufTemp += 4;
			 if(random==13){
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			}
			pBufTemp += 4;
			 if(random==14){
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			}
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 4
			 if(random==15){
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			}
		  	pBufTemp += 4;
			 if(random==16){
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			}
			pBufTemp += 4;
			 if(random==17){
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			}
			pBufTemp += 4;
			 if(random==18){
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			}
			pBufTemp += 4;
			 if(random==19){
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			}
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 5
			 if(random==20){
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			}
		  	pBufTemp += 4;
			 if(random==21){
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			}
			pBufTemp += 4;
			 if(random==22){
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			}
			pBufTemp += 4;
			 if(random==23){
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			}
			pBufTemp += 4;
			 if(random==24){
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			}
			
			
			
			
			pBufTemp = pBufTemp - (buf_line_size << 2) - 12;
			
			
			if(pixelR<0) pixelR *= (-1);
			if(pixelG<0) pixelG *= (-1);
			if(pixelB<0) pixelB *= (-1);

			//total += sobel;
			
			*(pTemp+0) = pixelR;
			*(pTemp+1) = pixelG;
			*(pTemp+2) = pixelB;
			pTemp += 4;
		}
		//pTemp +=16 ;
		pBufTemp += 16;
	} 
	return;
	
}
void iPhonePhotoFilterLV03(unsigned char* pSrc,unsigned int width,unsigned int height){
	unsigned int line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;//[height][width*4];
	line_size=width*4;
	unsigned char temp;
	pBuf=(unsigned char*)malloc(height*line_size);
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			*(pBuf+w+h*line_size)= *(pTemp+w+h*line_size);
		}
	}

	
	iPhonePhotoPrewittFilter((unsigned char*)pBuf,width,height);
	iPhonePhotoInvertFilter((unsigned char*)pBuf,width,height);
	
	
	
	pTemp=pSrc;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			
			
			temp =  *(pTemp+w+h*line_size)* (*(pBuf+w+h*line_size))/255;
			if((temp *50+(*(pBuf+w+h*line_size))*150)/165<255)(*(pBuf+w+h*line_size))=( temp *50+(*(pBuf+w+h*line_size))*150)/165;
			else (*(pBuf+w+h*line_size))=255;
		}
	}
	
	iPhonePhotoMotionBlurFilter((unsigned char*)pBuf,width,height);



	
	pTemp=pSrc;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			*(pTemp+w+h*line_size) =( *(pTemp+w+h*line_size)*70+ (*(pBuf+w+h*line_size))*185)/255;
		}
	}
	if(pBuf!=NULL)free(pBuf);
	
	
}
void iPhonePhotoFilterLV03_1(unsigned char* pSrc,unsigned int width,unsigned int height){
	unsigned int line_size;
	unsigned char* pTemp;

//	unsigned char pBuf[height][width*4];
	unsigned char pBuf2[height][width*4];
	line_size=width*4;
	
	//	buf_size=height*line_size;
	pTemp=pSrc;

	
	

	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			pBuf2[h][w] = *(pTemp+w+h*line_size);
		}
	}
		iPhonePhotoPrewittFilter((unsigned char*)pBuf2,width,height);
		iPhonePhotoInvertFilter((unsigned char*)pBuf2,width,height);



	pTemp=pSrc;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
		

			 *(pTemp+w+h*line_size) =  *(pTemp+w+h*line_size)* pBuf2[h][w]/255;
			if((*(pTemp+w+h*line_size)*50+pBuf2[h][w]*150)/165<255)*(pTemp+w+h*line_size)=( *(pTemp+w+h*line_size)*50+pBuf2[h][w]*150)/165;
			else *(pTemp+w+h*line_size)=255;
		}
	}

	
}

void iPhonePhotoInvertFilter(unsigned char* pSrc,unsigned int width,unsigned int height){
	int buf_size;
	unsigned char* p_temp;
	p_temp=pSrc;
	buf_size=width*height;
	for(int i=0;i<buf_size;i++,p_temp+=4){
		*p_temp=255-*p_temp;
		*(p_temp+1)=255-*(p_temp+1);
		*(p_temp+2)=255-*(p_temp+2)	;
	}
	

}

void iPhonePhotoSharpFilter(unsigned char* pSrc,unsigned int width,unsigned int height){
	
	unsigned int line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char pBuf[height+2][width*4+8];
		int pixelR,pixelG,pixelB;
	line_size=width*4;
	buf_line_size=width*4+8;
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	for(int w=0; w<(width*4); w++)
	{
		pBuf[0][w+4] =*(pTemp+w);
	}
	
	
	for(int w=0; w<(width*4); w++)
	{
		pBuf[height+1][w+4] =*(pTemp+w+(height-1)*line_size);
	}
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<4; w++)
		{
			pBuf[h+1][w] = *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<4; w++)
		{
			pBuf[h+1][w+4+width*4] =*(pTemp+(width-1)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			pBuf[h+1][w+4] = *(pTemp+w+h*line_size);
		}
	}
	
	
	pTemp=pSrc;
	
	//pTemp+=line_size*2+8;
	for(int h=0; h<height; h++)
	{
		for(int w=0; w<width; w++)
		{
			
 			pixelR = 0;
			pixelG = 0;
			pixelB = 0;
			// line - 1
		 	pixelR += (-1)*(*pBufTemp);
			pixelG += (-1)*(*(pBufTemp+1));
			pixelB += (-1)*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	pixelR += (-1)*(*pBufTemp);
			pixelG += (-1)*(*(pBufTemp+1));
			pixelB += (-1)*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += (-1)*(*pBufTemp);
			pixelG += (-1)*(*(pBufTemp+1));
			pixelB += (-1)*(*(pBufTemp+2));
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 2
			pixelR += (-1)*(*pBufTemp);
			pixelG += (-1)*(*(pBufTemp+1));
			pixelB += (-1)*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	pixelR += 9*(*pBufTemp);
			pixelG += 9*(*(pBufTemp+1));
			pixelB += 9*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += (-1)*(*pBufTemp);
			pixelG += (-1)*(*(pBufTemp+1));
			pixelB += (-1)*(*(pBufTemp+2));
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 3
			pixelR += (-1)*(*pBufTemp);
			pixelG += (-1)*(*(pBufTemp+1));
			pixelB += (-1)*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	pixelR += (-1)*(*pBufTemp);
			pixelG += (-1)*(*(pBufTemp+1));
			pixelB += (-1)*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += (-1)*(*pBufTemp);
			pixelG += (-1)*(*(pBufTemp+1));
			pixelB += (-1)*(*(pBufTemp+2));
			
			
			pBufTemp = pBufTemp - (buf_line_size << 1) - 4;
			
			
			if(pixelR<0) pixelR *= (-1);
			if(pixelG<0) pixelG *= (-1);
			if(pixelB<0) pixelB *= (-1);
			if(pixelR>255) pixelR =255;
			if(pixelG>255) pixelG =255;
			if(pixelB>255) pixelB =255;
			//total += sobel;
			
			*(pTemp+0) = pixelR;
			*(pTemp+1) = pixelG;
			*(pTemp+2) = pixelB;
			pTemp += 4;
		}
		//pTemp +=16 ;
		pBufTemp += 8;
	} 
	return;
	
	
}


void iPhonePhotoKirschFilter(unsigned char* pSrc,unsigned int width,unsigned int height){
	
	unsigned int line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char pBuf[height+2][width*4+8];
	int KirschR,KirschG,KirschB;
	int N_pixelR,N_pixelG,N_pixelB;
	int E_pixelR,E_pixelG,E_pixelB;
	int W_pixelR,W_pixelG,W_pixelB;
	int S_pixelR,S_pixelG,S_pixelB;
	line_size=width*4;
	buf_line_size=width*4+8;
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	
	for(int w=0; w<(width*4); w++)
	{
		pBuf[0][w+4] =*(pTemp+w);
	}
	
	
	for(int w=0; w<(width*4); w++)
	{
		pBuf[height+1][w+4] =*(pTemp+w+(height-1)*line_size);
	}
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<4; w++)
		{
			pBuf[h+1][w] = *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<4; w++)
		{
			pBuf[h+1][w+4+width*4] =*(pTemp+(width-1)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			pBuf[h+1][w+4] = *(pTemp+w+h*line_size);
		}
	}
	
	
	pTemp=pSrc;
	for(int h=0; h<height; h++)
	{
		for(int w=0; w<width; w++)
		{
			
 			N_pixelR = 0;
			N_pixelG = 0;
			N_pixelB = 0;
			// line - 1
		 	N_pixelR += (-3)*(*pBufTemp);
			N_pixelG += (-3)*(*(pBufTemp+1));
			N_pixelB += (-3)*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	N_pixelR += (-3)*(*pBufTemp);
			N_pixelG += (-3)*(*(pBufTemp+1));
			N_pixelB += (-3)*(*(pBufTemp+2));
			pBufTemp += 4;
			N_pixelR += (-3)*(*pBufTemp);
			N_pixelG += (-3)*(*(pBufTemp+1));
			N_pixelB += (-3)*(*(pBufTemp+2));
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 2
			N_pixelR += (-3)*(*pBufTemp);
			N_pixelG += (-3)*(*(pBufTemp+1));
			N_pixelB += (-3)*(*(pBufTemp+2));
		  	pBufTemp += 4;

			pBufTemp += 4;
			N_pixelR += (-3)*(*pBufTemp);
			N_pixelG += (-3)*(*(pBufTemp+1));
			N_pixelB += (-3)*(*(pBufTemp+2));
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 3
			N_pixelR += (5)*(*pBufTemp);
			N_pixelG += (5)*(*(pBufTemp+1));
			N_pixelB += (5)*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	N_pixelR += (5)*(*pBufTemp);
			N_pixelG += (5)*(*(pBufTemp+1));
			N_pixelB += (5)*(*(pBufTemp+2));
			pBufTemp += 4;
			N_pixelR += (5)*(*pBufTemp);
			N_pixelG += (5)*(*(pBufTemp+1));
			N_pixelB += (5)*(*(pBufTemp+2));
			pBufTemp = pBufTemp - (buf_line_size << 1) - 8;
			//
			E_pixelR = 0;
			E_pixelG = 0;
			E_pixelB = 0;
			// line - 1
		 	E_pixelR += (5)*(*pBufTemp);
			E_pixelG += (5)*(*(pBufTemp+1));
			E_pixelB += (5)*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	E_pixelR += (-3)*(*pBufTemp);
			E_pixelG += (-3)*(*(pBufTemp+1));
			E_pixelB += (-3)*(*(pBufTemp+2));
			pBufTemp += 4;
			E_pixelR += (-3)*(*pBufTemp);
			E_pixelG += (-3)*(*(pBufTemp+1));
			E_pixelB += (-3)*(*(pBufTemp+2));
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 2
			E_pixelR += (5)*(*pBufTemp);
			E_pixelG += (5)*(*(pBufTemp+1));
			E_pixelB += (5)*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	
			pBufTemp += 4;
			E_pixelR += (-3)*(*pBufTemp);
			E_pixelG += (-3)*(*(pBufTemp+1));
			E_pixelB += (-3)*(*(pBufTemp+2));
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 3
			E_pixelR += (5)*(*pBufTemp);
			E_pixelG += (5)*(*(pBufTemp+1));
			E_pixelB += (5)*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	E_pixelR += (-3)*(*pBufTemp);
			E_pixelG += (-3)*(*(pBufTemp+1));
			E_pixelB += (-3)*(*(pBufTemp+2));
			pBufTemp += 4;
			E_pixelR += (-3)*(*pBufTemp);
			E_pixelG += (-3)*(*(pBufTemp+1));
			E_pixelB += (-3)*(*(pBufTemp+2));
			pBufTemp = pBufTemp - (buf_line_size << 1) - 8;
			// 			
			W_pixelR = 0;
			W_pixelG = 0;
			W_pixelB = 0;
			// line - 1
		 	W_pixelR += (-3)*(*pBufTemp);
			W_pixelG += (-3)*(*(pBufTemp+1));
			W_pixelB += (-3)*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	W_pixelR += (-3)*(*pBufTemp);
			W_pixelG += (-3)*(*(pBufTemp+1));
			W_pixelB += (-3)*(*(pBufTemp+2));
			pBufTemp += 4;
			W_pixelR += (5)*(*pBufTemp);
			W_pixelG += (5)*(*(pBufTemp+1));
			W_pixelB += (5)*(*(pBufTemp+2));
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 2
			W_pixelR += (-3)*(*pBufTemp);
			W_pixelG += (-3)*(*(pBufTemp+1));
			W_pixelB += (-3)*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	
			pBufTemp += 4;
			W_pixelR += (5)*(*pBufTemp);
			W_pixelG += (5)*(*(pBufTemp+1));
			W_pixelB += (5)*(*(pBufTemp+2));
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 3
			W_pixelR += (-3)*(*pBufTemp);
			W_pixelG += (-3)*(*(pBufTemp+1));
			W_pixelB += (-3)*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	W_pixelR += (-3)*(*pBufTemp);
			W_pixelG += (-3)*(*(pBufTemp+1));
			W_pixelB += (-3)*(*(pBufTemp+2));
			pBufTemp += 4;
			W_pixelR += (5)*(*pBufTemp);
			W_pixelG += (5)*(*(pBufTemp+1));
			W_pixelB += (5)*(*(pBufTemp+2));
			pBufTemp = pBufTemp - (buf_line_size << 1) - 8;
			// 			
			S_pixelR = 0;
			S_pixelG = 0;
			S_pixelB = 0;
			// line - 1
		 	S_pixelR += (5)*(*pBufTemp);
			S_pixelG += (5)*(*(pBufTemp+1));
			S_pixelB += (5)*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	S_pixelR += (5)*(*pBufTemp);
			S_pixelG += (5)*(*(pBufTemp+1));
			S_pixelB += (5)*(*(pBufTemp+2));
			pBufTemp += 4;
			S_pixelR += (5)*(*pBufTemp);
			S_pixelG += (5)*(*(pBufTemp+1));
			S_pixelB += (5)*(*(pBufTemp+2));
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 2
			S_pixelR += (-3)*(*pBufTemp);
			S_pixelG += (-3)*(*(pBufTemp+1));
			S_pixelB += (-3)*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	
			pBufTemp += 4;
			S_pixelR += (-3)*(*pBufTemp);
			S_pixelG += (-3)*(*(pBufTemp+1));
			S_pixelB += (-3)*(*(pBufTemp+2));
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 3
			S_pixelR += (-3)*(*pBufTemp);
			S_pixelG += (-3)*(*(pBufTemp+1));
			S_pixelB += (-3)*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	S_pixelR += (-3)*(*pBufTemp);
			S_pixelG += (-3)*(*(pBufTemp+1));
			S_pixelB += (-3)*(*(pBufTemp+2));
			pBufTemp += 4;
			S_pixelR += (-3)*(*pBufTemp);
			S_pixelG += (-3)*(*(pBufTemp+1));
			S_pixelB += (-3)*(*(pBufTemp+2));
			pBufTemp = pBufTemp - (buf_line_size << 1) - 4;
			//
			KirschR = N_pixelR + E_pixelR + W_pixelR + S_pixelR;
			KirschG = N_pixelG + E_pixelG + W_pixelG + S_pixelG;
			KirschB = N_pixelB + E_pixelB + W_pixelB + S_pixelB;
			if(KirschR<0) KirschR *= (-1);
			if(KirschG<0) KirschG *= (-1);
			if(KirschB<0) KirschB *= (-1);
			
			//total += sobel;
			
			*(pTemp+0) = KirschR;
			*(pTemp+1) = KirschG;
			*(pTemp+2) = KirschB;
			pTemp += 4;
		}
		//pTemp +=16 ;
		pBufTemp += 8;
	} 
	return;
	
	
}
void iPhonePhotoBinaryFilterGray1(unsigned char* pSrc,unsigned int width,unsigned int height){
	int buf_size;
	unsigned char* p_temp;
	p_temp=pSrc;
	buf_size=width*height*4;
	
	for(int i=0;i<buf_size;i++,p_temp++){
		if((*p_temp)<220) *p_temp=0;
		else *p_temp=255;
		
	}
}
void iPhonePhotoBinaryFilterGray2(unsigned char* pSrc,unsigned int width,unsigned int height){
	int buf_size;
	unsigned char* p_temp;
	p_temp=pSrc;
	buf_size=width*height*4;
	
	for(int i=0;i<buf_size;i++,p_temp++){
		if((*p_temp)<100) *p_temp=0;
		else *p_temp=255;
		
	}
}


void iPhonePhotoPrewittFilter(unsigned char* pSrc,unsigned int width,unsigned int height){
	unsigned int buf_size,line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;//[height+2][width*4+8];
	int PrewittR,PrewittG,PrewittB;
	int cols_pixelR,cols_pixelG,cols_pixelB;
	int row_pixelR,row_pixelG,row_pixelB;
	line_size=width*4;
	buf_line_size=width*4+8;
	buf_size=height*line_size;
	pTemp=pSrc;
	pBuf=(unsigned char*)malloc((height+2)*buf_line_size);
	pBufTemp=(unsigned char*) pBuf;
	
	for(int w=0; w<(width*4); w++)
	{
		*(pBuf+w+4)=*(pTemp+w);
	}
	
	
	for(int w=0; w<(width*4); w++)
	{
//		pBuf[height+1][w+4] 
		*(pBuf+w+4+(height+1)*buf_line_size)=*(pTemp+w+(height-1)*line_size);
	}
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<4; w++)
		{
			//pBuf[h+1][w] 
			*(pBuf+(h+1)*buf_line_size+w)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<4; w++)
		{
			//pBuf[h+1][w+4+width*4] 
			*(pBuf+(h+1)*buf_line_size+w+4+width*4)=*(pTemp+(width-1)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//pBuf[h+1][w+4] 
			*(pBuf+(h+1)*buf_line_size+w+4)= *(pTemp+w+h*line_size);
		}
	}
	
	
	pTemp=pSrc;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width); w++)
		{
 			row_pixelR = 0;
			row_pixelG = 0;
			row_pixelB = 0;
			// line - 1
			row_pixelR += *pBufTemp;
			row_pixelG += *(pBufTemp+1);
			row_pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	row_pixelR += *pBufTemp;
			row_pixelG += *(pBufTemp+1);
			row_pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			row_pixelR += *pBufTemp;
			row_pixelG += *(pBufTemp+1);
			row_pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 2
			pBufTemp += 4;
			pBufTemp += 4;
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 3
			row_pixelR -= *pBufTemp;
			row_pixelG -= *(pBufTemp+1);
			row_pixelB -= *(pBufTemp+2);
		  	pBufTemp += 4;
		 	row_pixelR -= *pBufTemp;
			row_pixelG -= *(pBufTemp+1);
			row_pixelB -= *(pBufTemp+2);
			pBufTemp += 4;
			row_pixelR -= *pBufTemp;
			row_pixelG -= *(pBufTemp+1);
			row_pixelB -= *(pBufTemp+2);
			pBufTemp = pBufTemp - (buf_line_size << 1) - 8;
			
			cols_pixelR = 0;
			cols_pixelG = 0;
			cols_pixelB = 0;
			// line - 1
			cols_pixelR += *pBufTemp;
			cols_pixelG += *(pBufTemp+1);
			cols_pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
			pBufTemp += 4;
			cols_pixelR -= *pBufTemp;
			cols_pixelG -= *(pBufTemp+1);
			cols_pixelB -= *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 2
			cols_pixelR += *pBufTemp;
			cols_pixelG += *(pBufTemp+1);
			cols_pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
			pBufTemp += 4;
			cols_pixelR -= *pBufTemp;
			cols_pixelG -= *(pBufTemp+1);
			cols_pixelB -= *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 3
			cols_pixelR += *pBufTemp;
			cols_pixelG += *(pBufTemp+1);
			cols_pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
			pBufTemp += 4;
			cols_pixelR -= *pBufTemp;
			cols_pixelG -= *(pBufTemp+1);
			cols_pixelB -= *(pBufTemp+2);
			pBufTemp = pBufTemp - (buf_line_size << 1) - 4;
			
			if(row_pixelR<0) row_pixelR *= (-1);
			if(row_pixelG<0) row_pixelG *= (-1);
			if(row_pixelB<0) row_pixelB *= (-1);
			if(cols_pixelR<0) cols_pixelR *= (-1);
			if(cols_pixelG<0) cols_pixelG *= (-1);
			if(cols_pixelB<0) cols_pixelB *= (-1);
			PrewittR = row_pixelR + cols_pixelR;
			PrewittG = row_pixelG + cols_pixelG;
			PrewittB = row_pixelB + cols_pixelB;
			//if(PrewittR>255)PrewittR=255;
			//if(PrewittG>255)PrewittG=255;
			//if(PrewittB>255)PrewittB=255;
			//total += sobel;
			
			*(pTemp+0) = PrewittR;
			*(pTemp+1) = PrewittG;
			*(pTemp+2) = PrewittB;
			pTemp += 4;
		}
		pBufTemp += 8;
	} 
	if(pBuf!=NULL)free(pBuf);
	return;
	
}


void iPhonePhotoOliFilter2(unsigned char* pSrc,unsigned int width,unsigned int height){
	
	unsigned int line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;//[height+4][width*4+16];
	int pixelR1,pixelG1,pixelB1;
	int pixelR2,pixelG2,pixelB2;
	int pixelR3,pixelG3,pixelB3;
	int pixelR4,pixelG4,pixelB4;
	int random;
	line_size=width*4;
	buf_line_size=width*4+16;
	
	pBuf=(unsigned char*)malloc((height+4)*buf_line_size);
	
	//	buf_size=height*line_size;
	pTemp=pSrc;
//	pBufTemp=(unsigned char*) pBuf;
	
	//
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//			pBuf[h][w+8]
			*(pBuf+h*buf_line_size+w+8)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//			pBuf[h+height+2][w+8]
			*(pBuf+(h+height+2)*buf_line_size+w+8)= *(pTemp+w+(height-2+h)*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			//pBuf[h+2][w] 
			*(pBuf+(h+2)*buf_line_size+w)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			//	pBuf[h+2][w+8+width*4]
			*(pBuf+(h+2)*buf_line_size+w+8+width*4)= *(pTemp+(width-2)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//pBuf[h+2][w+8] 
			*(pBuf+(h+2)*buf_line_size+w+8)= *(pTemp+w+h*line_size);
		}
	}
	//

		pBufTemp= pBuf;
	//pTemp+=line_size*2+8;
	
	for(int h=0; h<(height/2); h++)
	{
		for(int w=0; w<=(width/2); w++)
		{
			
			random=rand()%9;
			
 			pixelR1 = 0;
			pixelG1 = 0;
			pixelB1 = 0;
			
			pixelR2 = 0;
			pixelG2 = 0;
			pixelB2 = 0;
			
			pixelR3 = 0;
			pixelG3 = 0;
			pixelB3 = 0;
			
			pixelR4 = 0;
			pixelG4 = 0;
			pixelB4 = 0;
			
			// line - 1
			if(random==0){
				pixelR1 += *pBufTemp;
				pixelG1 += *(pBufTemp+1);
				pixelB1 += *(pBufTemp+2);
				pBufTemp += 4;
				pixelR2 += *pBufTemp;
				pixelG2 += *(pBufTemp+1);
				pixelB2 += *(pBufTemp+2);
				pBufTemp = pBufTemp + buf_line_size - 4;
				pixelR3 += *pBufTemp;
				pixelG3 += *(pBufTemp+1);
				pixelB3 += *(pBufTemp+2);
				pBufTemp += 4;
				pixelR4 += *pBufTemp;
				pixelG4 += *(pBufTemp+1);
				pixelB4 += *(pBufTemp+2);
				pBufTemp = pBufTemp - buf_line_size - 4;
				
			}
			pBufTemp += 8;
			if(random==1){
				pixelR1 += *pBufTemp;
				pixelG1 += *(pBufTemp+1);
				pixelB1 += *(pBufTemp+2);
				pBufTemp += 4;
				pixelR2 += *pBufTemp;
				pixelG2 += *(pBufTemp+1);
				pixelB2 += *(pBufTemp+2);
				pBufTemp = pBufTemp + buf_line_size - 4;
				pixelR3 += *pBufTemp;
				pixelG3 += *(pBufTemp+1);
				pixelB3 += *(pBufTemp+2);
				pBufTemp += 4;
				pixelR4 += *pBufTemp;
				pixelG4 += *(pBufTemp+1);
				pixelB4 += *(pBufTemp+2);
				pBufTemp = pBufTemp - buf_line_size-4;

			}
			pBufTemp += 8;
			if(random==2){
				pixelR1 += *pBufTemp;
				pixelG1 += *(pBufTemp+1);
				pixelB1 += *(pBufTemp+2);
				pBufTemp += 4;
				if(w<(width/2)){
				pixelR2 += *pBufTemp;
				pixelG2 += *(pBufTemp+1);
				pixelB2 += *(pBufTemp+2);
				}
				pBufTemp = pBufTemp + buf_line_size - 4;
				pixelR3 += *pBufTemp;
				pixelG3 += *(pBufTemp+1);
				pixelB3 += *(pBufTemp+2);
				pBufTemp += 4;
				if(w<(width/2)){
				pixelR4 += *pBufTemp;
				pixelG4 += *(pBufTemp+1);
				pixelB4 += *(pBufTemp+2);
				}
				pBufTemp = pBufTemp - buf_line_size-4;
				
			}
			// line - 2
			pBufTemp = pBufTemp + buf_line_size*2-16 ;
			if(random==3){
				pixelR1 += *pBufTemp;
				pixelG1 += *(pBufTemp+1);
				pixelB1 += *(pBufTemp+2);
				pBufTemp += 4;
				pixelR2 += *pBufTemp;
				pixelG2 += *(pBufTemp+1);
				pixelB2 += *(pBufTemp+2);
				pBufTemp = pBufTemp + buf_line_size - 4;
				pixelR3 += *pBufTemp;
				pixelG3 += *(pBufTemp+1);
				pixelB3 += *(pBufTemp+2);
				pBufTemp += 4;
				pixelR4 += *pBufTemp;
				pixelG4 += *(pBufTemp+1);
				pixelB4 += *(pBufTemp+2);
				pBufTemp = pBufTemp - buf_line_size-4;
				
			}
			pBufTemp += 8;
			if(random==4){
				pixelR1 += *pBufTemp;
				pixelG1 += *(pBufTemp+1);
				pixelB1 += *(pBufTemp+2);
				pBufTemp += 4;
				pixelR2 += *pBufTemp;
				pixelG2 += *(pBufTemp+1);
				pixelB2 += *(pBufTemp+2);
				pBufTemp = pBufTemp + buf_line_size - 4;
				pixelR3 += *pBufTemp;
				pixelG3 += *(pBufTemp+1);
				pixelB3 += *(pBufTemp+2);
				pBufTemp += 4;
				pixelR4 += *pBufTemp;
				pixelG4 += *(pBufTemp+1);
				pixelB4 += *(pBufTemp+2);
				pBufTemp = pBufTemp - buf_line_size-4;
				
			}
			pBufTemp += 8;
			if(random==5){
				pixelR1 += *pBufTemp;
				pixelG1 += *(pBufTemp+1);
				pixelB1 += *(pBufTemp+2);
				pBufTemp += 4;
				if(w<(width/2)){
				pixelR2 += *pBufTemp;
				pixelG2 += *(pBufTemp+1);
				pixelB2 += *(pBufTemp+2);
				}
				pBufTemp = pBufTemp + buf_line_size - 4;
				pixelR3 += *pBufTemp;
				pixelG3 += *(pBufTemp+1);
				pixelB3 += *(pBufTemp+2);
				pBufTemp += 4;
				if(w<(width/2)){
				pixelR4 += *pBufTemp;
				pixelG4 += *(pBufTemp+1);
				pixelB4 += *(pBufTemp+2);
				}
				pBufTemp = pBufTemp - buf_line_size-4;
				
			}
			// line - 3
			pBufTemp = pBufTemp + buf_line_size*2-16;
			if(random==6){
				pixelR1 += *pBufTemp;
				pixelG1 += *(pBufTemp+1);
				pixelB1 += *(pBufTemp+2);
				pBufTemp += 4;
				pixelR2 += *pBufTemp;
				pixelG2 += *(pBufTemp+1);
				pixelB2 += *(pBufTemp+2);
				pBufTemp = pBufTemp + buf_line_size - 4;
				if(h<(height/2)){
				pixelR3 += *pBufTemp;
				pixelG3 += *(pBufTemp+1);
				pixelB3 += *(pBufTemp+2);
				}
				pBufTemp += 4;
				if(h<(height/2)){
				
				pixelR4 += *pBufTemp;
				pixelG4 += *(pBufTemp+1);
				pixelB4 += *(pBufTemp+2);
				}
				pBufTemp = pBufTemp - buf_line_size-4;
				
			}
			pBufTemp += 8;
			if(random==7){
				pixelR1 += *pBufTemp;
				pixelG1 += *(pBufTemp+1);
				pixelB1 += *(pBufTemp+2);
				pBufTemp += 4;
				pixelR2 += *pBufTemp;
				pixelG2 += *(pBufTemp+1);
				pixelB2 += *(pBufTemp+2);
				pBufTemp = pBufTemp + buf_line_size - 4;
				if(h<(height/2)){
				pixelR3 += *pBufTemp;
				pixelG3 += *(pBufTemp+1);
				pixelB3 += *(pBufTemp+2);
				}
				pBufTemp += 4;
				if(h<(height/2)){
				pixelR4 += *pBufTemp;
				pixelG4 += *(pBufTemp+1);
				pixelB4 += *(pBufTemp+2);
				}
				pBufTemp = pBufTemp - buf_line_size-4;
				
			}
			pBufTemp += 8;
			if(random==8){
				pixelR1 += *pBufTemp;
				pixelG1 += *(pBufTemp+1);
				pixelB1 += *(pBufTemp+2);
				pBufTemp += 4;
				if(w<(width/2)){
				pixelR2 += *pBufTemp;
				pixelG2 += *(pBufTemp+1);
				pixelB2 += *(pBufTemp+2);
				}
				pBufTemp = pBufTemp + buf_line_size - 4;
				if(h<(height/2)){
				pixelR3 += *pBufTemp;
				pixelG3 += *(pBufTemp+1);
				pixelB3 += *(pBufTemp+2);
				}
				pBufTemp += 4;
				if(w<(width/2)){
					if(h<(height/2)){
				pixelR4 += *pBufTemp;
				pixelG4 += *(pBufTemp+1);
				pixelB4 += *(pBufTemp+2);
					}
				}
				pBufTemp = pBufTemp - buf_line_size-4;
				
			}
			
			pBufTemp = pBufTemp - buf_line_size*4 - 8;
			
			
			if(pixelR1<0) pixelR1 *= (-1);
			if(pixelG1<0) pixelG1 *= (-1);
			if(pixelB1<0) pixelB1 *= (-1);
			
			if(pixelR2<0) pixelR2 *= (-1);
			if(pixelG2<0) pixelG2 *= (-1);
			if(pixelB2<0) pixelB2 *= (-1);
			
			if(pixelR3<0) pixelR3 *= (-1);
			if(pixelG3<0) pixelG3 *= (-1);
			if(pixelB3<0) pixelB3 *= (-1);
			
			if(pixelR4<0) pixelR4 *= (-1);
			if(pixelG4<0) pixelG4 *= (-1);
			if(pixelB4<0) pixelB4 *= (-1);
			
			//total += sobel;
			if(h<(height/2)||height%2){
				*(pTemp+0) = pixelR1;
				*(pTemp+1) = pixelG1;
				*(pTemp+2) = pixelB1;
				if(w<(width/2)){
					*(pTemp+4) = pixelR2;
					*(pTemp+5) = pixelG2;
					*(pTemp+6) = pixelB2;
				}
				if(h<(height/2)){
					if(w<(width/2)){
						*(pTemp+0+line_size) = pixelR3;
						*(pTemp+1+line_size) = pixelG3;
						*(pTemp+2+line_size) = pixelB3;
				
						*(pTemp+4+line_size) = pixelR4;
						*(pTemp+5+line_size) = pixelG4;
						*(pTemp+6+line_size) = pixelB4;
					}
				}
			}
			 
			pTemp += 8;
			
		}
		//pTemp +=16 ;
		if(width%2)pTemp -= 4;
		else pTemp -= 8;
		pTemp+= line_size;
		if(width%2)pBufTemp -= 4;
		else pBufTemp -= 8;
		pBufTemp += 16;
		pBufTemp +=buf_line_size;
	}
	if(pBuf!=NULL)free(pBuf);
	return;
	 
	
}

void iPhonePhotoOverlayFilter(unsigned char* pSrc,unsigned int width,unsigned int height){
	unsigned int line_size;
	unsigned char* pTemp;
	unsigned char temp;
	line_size=width*4;
	pTemp=pSrc;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			
			temp=*(pTemp+w+h*line_size);
			
			if(temp<=127){
				*(pTemp+w+h*line_size)=temp*(temp)/128;
			}
			else if(temp>=128){
				*(pTemp+w+h*line_size)=temp+(255-temp)*(temp-127)/128;
			}
		}
	}
	
}

void iPhonePhotoFilterLV01(unsigned char* pSrc,unsigned int width,unsigned int height){
	
	unsigned int line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;//[height+4][width*4+16];
	int pixelR,pixelG,pixelB;
	line_size=width*4;
	buf_line_size=width*4+16;
	pBuf=(unsigned char*)malloc(buf_line_size*(height+4));
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//			pBuf[h][w+8]
			*(pBuf+h*buf_line_size+w+8)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//			pBuf[h+height+2][w+8]
			*(pBuf+(h+height+2)*buf_line_size+w+8)= *(pTemp+w+(height-2+h)*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			//pBuf[h+2][w] 
			*(pBuf+(h+2)*buf_line_size+w)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			//	pBuf[h+2][w+8+width*4]
			*(pBuf+(h+2)*buf_line_size+w+8+width*4)= *(pTemp+(width-2)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//pBuf[h+2][w+8] 
			*(pBuf+(h+2)*buf_line_size+w+8)= *(pTemp+w+h*line_size);
		}
	}
	
	
	//pTemp+=line_size*2+8;
	for(int h=0; h<height; h++)
	{
		for(int w=0; w<width; w++)
		{
			
 			pixelR = 0;
			pixelG = 0;
			pixelB = 0;
			// line - 1
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += 4*(*pBufTemp);
			pixelG += 4*(*(pBufTemp+1));
			pixelB += 4*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 7*(*pBufTemp);
			pixelG += 7*(*(pBufTemp+1));
			pixelB += 7*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 4*(*pBufTemp);
			pixelG += 4*(*(pBufTemp+1));
			pixelB += 4*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 2
			pixelR += 4*(*pBufTemp);
			pixelG += 4*(*(pBufTemp+1));
			pixelB += 4*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	pixelR += 16*(*pBufTemp);
			pixelG += 16*(*(pBufTemp+1));
			pixelB += 16*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 26*(*pBufTemp);
			pixelG += 26*(*(pBufTemp+1));
			pixelB += 26*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 16*(*pBufTemp);
			pixelG += 16*(*(pBufTemp+1));
			pixelB += 16*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 4*(*pBufTemp);
			pixelG += 4*(*(pBufTemp+1));
			pixelB += 4*(*(pBufTemp+2));
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 3
			pixelR += 7*(*pBufTemp);
			pixelG += 7*(*(pBufTemp+1));
			pixelB += 7*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	pixelR += 26*(*pBufTemp);
			pixelG += 26*(*(pBufTemp+1));
			pixelB += 26*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 41*(*pBufTemp);
			pixelG += 41*(*(pBufTemp+1));
			pixelB += 41*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 26*(*pBufTemp);
			pixelG += 26*(*(pBufTemp+1));
			pixelB += 26*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 7*(*pBufTemp);
			pixelG += 7*(*(pBufTemp+1));
			pixelB += 7*(*(pBufTemp+2));
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 4
			pixelR += 4*(*pBufTemp);
			pixelG += 4*(*(pBufTemp+1));
			pixelB += 4*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	pixelR += 16*(*pBufTemp);
			pixelG += 16*(*(pBufTemp+1));
			pixelB += 16*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 26*(*pBufTemp);
			pixelG += 26*(*(pBufTemp+1));
			pixelB += 26*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 16*(*pBufTemp);
			pixelG += 16*(*(pBufTemp+1));
			pixelB += 16*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 4*(*pBufTemp);
			pixelG += 4*(*(pBufTemp+1));
			pixelB += 4*(*(pBufTemp+2));
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 5
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += 4*(*pBufTemp);
			pixelG += 4*(*(pBufTemp+1));
			pixelB += 4*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 7*(*pBufTemp);
			pixelG += 7*(*(pBufTemp+1));
			pixelB += 7*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += 4*(*pBufTemp);
			pixelG += 4*(*(pBufTemp+1));
			pixelB += 4*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			
			
			
			pBufTemp = pBufTemp - (buf_line_size << 2) - 12;
			
			
			if(pixelR<0) pixelR *= (-1);
			if(pixelG<0) pixelG *= (-1);
			if(pixelB<0) pixelB *= (-1);
			
			pixelR/=273;
			pixelG/=273;
			pixelB/=273;
			
			if(pixelR<=127){
				*(pTemp+0)=(pixelR)*(pixelR)/128;
			}
			else if(pixelR>=128){
				*(pTemp+0)=(pixelR)+(255-pixelR)*(pixelR-127)/128;
			}
			if(pixelG<=127){
				*(pTemp+1)=(pixelG)*(pixelG)/128;
			}
			else if(pixelG>=128){
				*(pTemp+1)=(pixelG)+(255-pixelG)*(pixelG-127)/128;
			}
			if(pixelB<=127){
				*(pTemp+2)=(pixelB)*(pixelB)/128;
			}
			else if(pixelB>=128){
				*(pTemp+2)=(pixelB)+(255-pixelB)*(pixelB-127)/128;
			}
			/*
			*(pTemp+0) = sobelR;
			*(pTemp+1) = sobelG;
			*(pTemp+2) = sobelB;
			 */
			pTemp += 4;
		}
		//pTemp +=16 ;
		pBufTemp += 16;
	} 
	if(pBuf)free(pBuf);
	/*
	unsigned int line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;//[height][width*4];
	line_size=width*4;
	
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	pBuf=(unsigned char*)malloc(height*line_size);
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			*(pBuf+w+h*line_size) = *(pTemp+w+h*line_size);
		}
	}
iPhonePhotoGaussianFilter5x5_x((unsigned char*)pBuf,width,height);


	pTemp=pSrc;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
	
			if((*(pBuf+w+h*line_size))<=127){
				*(pTemp+w+h*line_size)=(*(pBuf+w+h*line_size))*(*(pBuf+w+h*line_size))/128;
			}
			else if((*(pBuf+w+h*line_size))>=128){
				*(pTemp+w+h*line_size)=(*(pBuf+w+h*line_size))+(255-((*(pBuf+w+h*line_size))))*((*(pBuf+w+h*line_size))-127)/128;
			}

		}
	}
	 */
	
}


void iPhonePhotoSobelFilter5x5_x(unsigned char* pSrc,unsigned int width,unsigned int height){
	
	unsigned int line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;
	int sobelR,sobelG,sobelB;
	int cols_pixelR,cols_pixelG,cols_pixelB;
	int row_pixelR,row_pixelG,row_pixelB;
	line_size=width*4;
	buf_line_size=width*4+16;
	//	buf_size=height*line_size;
	pBuf=(unsigned char*)malloc((height+4)*buf_line_size);
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
//			pBuf[h][w+8]
			*(pBuf+h*buf_line_size+w+8)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
//			pBuf[h+height+2][w+8]
			*(pBuf+(h+height+2)*buf_line_size+w+8)= *(pTemp+w+(height-2+h)*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			//pBuf[h+2][w] 
			*(pBuf+(h+2)*buf_line_size+w)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
		//	pBuf[h+2][w+8+width*4]
			*(pBuf+(h+2)*buf_line_size+w+8+width*4)= *(pTemp+(width-2)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//pBuf[h+2][w+8] 
			*(pBuf+(h+2)*buf_line_size+w+8)= *(pTemp+w+h*line_size);
		}
	}
	
	
	//pTemp+=line_size*2+8;
	for(int h=0; h<height; h++)
	{
		for(int w=0; w<width; w++)
		{
			
 			row_pixelR = 0;
			row_pixelG = 0;
			row_pixelB = 0;
			// line - 1
			row_pixelR += *pBufTemp;
			row_pixelG += *(pBufTemp+1);
			row_pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	row_pixelR += 4*(*pBufTemp);
			row_pixelG += 4*(*(pBufTemp+1));
			row_pixelB += 4*(*(pBufTemp+2));
			pBufTemp += 4;
			row_pixelR += 6*(*pBufTemp);
			row_pixelG += 6*(*(pBufTemp+1));
			row_pixelB += 6*(*(pBufTemp+2));
			pBufTemp += 4;
			row_pixelR += 4*(*pBufTemp);
			row_pixelG += 4*(*(pBufTemp+1));
			row_pixelB += 4*(*(pBufTemp+2));
			pBufTemp += 4;
			row_pixelR += *pBufTemp;
			row_pixelG += *(pBufTemp+1);
			row_pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 2
		 	row_pixelR += 2*(*pBufTemp);
			row_pixelG += 2*(*(pBufTemp+1));
			row_pixelB += 2*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	row_pixelR += 8*(*pBufTemp);
			row_pixelG += 8*(*(pBufTemp+1));
			row_pixelB += 8*(*(pBufTemp+2));
			pBufTemp += 4;
			row_pixelR += 12*(*pBufTemp);
			row_pixelG += 12*(*(pBufTemp+1));
			row_pixelB += 12*(*(pBufTemp+2));
			pBufTemp += 4;
			row_pixelR += 8*(*pBufTemp);
			row_pixelG += 8*(*(pBufTemp+1));
			row_pixelB += 8*(*(pBufTemp+2));
			pBufTemp += 4;
		 	row_pixelR += 2*(*pBufTemp);
			row_pixelG += 2*(*(pBufTemp+1));
			row_pixelB += 2*(*(pBufTemp+2));
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 3

		  	pBufTemp += 4;

			pBufTemp += 4;

			pBufTemp += 4;

			pBufTemp += 4;

			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 4
		 	row_pixelR -= 2*(*pBufTemp);
			row_pixelG -= 2*(*(pBufTemp+1));
			row_pixelB -= 2*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	row_pixelR -= 8*(*pBufTemp);
			row_pixelG -= 8*(*(pBufTemp+1));
			row_pixelB -= 8*(*(pBufTemp+2));
			pBufTemp += 4;
			row_pixelR -= 12*(*pBufTemp);
			row_pixelG -= 12*(*(pBufTemp+1));
			row_pixelB -= 12*(*(pBufTemp+2));
			pBufTemp += 4;
			row_pixelR -= 8*(*pBufTemp);
			row_pixelG -= 8*(*(pBufTemp+1));
			row_pixelB -= 8*(*(pBufTemp+2));
			pBufTemp += 4;
		 	row_pixelR -= 2*(*pBufTemp);
			row_pixelG -= 2*(*(pBufTemp+1));
			row_pixelB -= 2*(*(pBufTemp+2));
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 5
			row_pixelR -= *pBufTemp;
			row_pixelG -= *(pBufTemp+1);
			row_pixelB -= *(pBufTemp+2);
		  	pBufTemp += 4;
		 	row_pixelR -= 4*(*pBufTemp);
			row_pixelG -= 4*(*(pBufTemp+1));
			row_pixelB -= 4*(*(pBufTemp+2));
			pBufTemp += 4;
			row_pixelR -= 6*(*pBufTemp);
			row_pixelG -= 6*(*(pBufTemp+1));
			row_pixelB -= 6*(*(pBufTemp+2));
			pBufTemp += 4;
			row_pixelR -= 4*(*pBufTemp);
			row_pixelG -= 4*(*(pBufTemp+1));
			row_pixelB -= 4*(*(pBufTemp+2));
			pBufTemp += 4;
			row_pixelR -= *pBufTemp;
			row_pixelG -= *(pBufTemp+1);
			row_pixelB -= *(pBufTemp+2);
			
			
			
			pBufTemp = pBufTemp - (buf_line_size << 2) - 16;
			//
			cols_pixelR = 0;
			cols_pixelG = 0;
			cols_pixelB = 0;
			// line - 1
			cols_pixelR += *pBufTemp;
			cols_pixelG += *(pBufTemp+1);
			cols_pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	cols_pixelR += 2*(*pBufTemp);
			cols_pixelG += 2*(*(pBufTemp+1));
			cols_pixelB += 2*(*(pBufTemp+2));
			pBufTemp += 4;

			pBufTemp += 4;
			cols_pixelR -= 2*(*pBufTemp);
			cols_pixelG -= 2*(*(pBufTemp+1));
			cols_pixelB -= 2*(*(pBufTemp+2));
			pBufTemp += 4;
			cols_pixelR -= *pBufTemp;
			cols_pixelG -= *(pBufTemp+1);
			cols_pixelB -= *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 2
		 	cols_pixelR += 4*(*pBufTemp);
			cols_pixelG += 4*(*(pBufTemp+1));
			cols_pixelB += 4*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	cols_pixelR += 8*(*pBufTemp);
			cols_pixelG += 8*(*(pBufTemp+1));
			cols_pixelB += 8*(*(pBufTemp+2));
			pBufTemp += 4;
			
			pBufTemp += 4;
			cols_pixelR -= 8*(*pBufTemp);
			cols_pixelG -= 8*(*(pBufTemp+1));
			cols_pixelB -= 8*(*(pBufTemp+2));
			pBufTemp += 4;
			cols_pixelR -= 4*(*pBufTemp);
			cols_pixelG -= 4*(*(pBufTemp+1));
			cols_pixelB -= 4*(*(pBufTemp+2));
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 3
		 	cols_pixelR += 6*(*pBufTemp);
			cols_pixelG += 6*(*(pBufTemp+1));
			cols_pixelB += 6*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	cols_pixelR += 12*(*pBufTemp);
			cols_pixelG += 12*(*(pBufTemp+1));
			cols_pixelB += 12*(*(pBufTemp+2));
			pBufTemp += 4;
			
			pBufTemp += 4;
			cols_pixelR -= 12*(*pBufTemp);
			cols_pixelG -= 12*(*(pBufTemp+1));
			cols_pixelB -= 12*(*(pBufTemp+2));
			pBufTemp += 4;
			cols_pixelR -= 6*(*pBufTemp);
			cols_pixelG -= 6*(*(pBufTemp+1));
			cols_pixelB -= 6*(*(pBufTemp+2));
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 4
		 	cols_pixelR += 4*(*pBufTemp);
			cols_pixelG += 4*(*(pBufTemp+1));
			cols_pixelB += 4*(*(pBufTemp+2));
		  	pBufTemp += 4;
		 	cols_pixelR += 8*(*pBufTemp);
			cols_pixelG += 8*(*(pBufTemp+1));
			cols_pixelB += 8*(*(pBufTemp+2));
			pBufTemp += 4;
			
			pBufTemp += 4;
			cols_pixelR -= 8*(*pBufTemp);
			cols_pixelG -= 8*(*(pBufTemp+1));
			cols_pixelB -= 8*(*(pBufTemp+2));
			pBufTemp += 4;
			cols_pixelR -= 4*(*pBufTemp);
			cols_pixelG -= 4*(*(pBufTemp+1));
			cols_pixelB -= 4*(*(pBufTemp+2));
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 5
			cols_pixelR += *pBufTemp;
			cols_pixelG += *(pBufTemp+1);
			cols_pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	cols_pixelR += 2*(*pBufTemp);
			cols_pixelG += 2*(*(pBufTemp+1));
			cols_pixelB += 2*(*(pBufTemp+2));
			pBufTemp += 4;
			
			pBufTemp += 4;
			cols_pixelR -= 2*(*pBufTemp);
			cols_pixelG -= 2*(*(pBufTemp+1));
			cols_pixelB -= 2*(*(pBufTemp+2));
			pBufTemp += 4;
			cols_pixelR -= *pBufTemp;
			cols_pixelG -= *(pBufTemp+1);
			cols_pixelB -= *(pBufTemp+2);
			
			
			
			pBufTemp = pBufTemp - (buf_line_size << 2) - 12;
			
			
			
			
			sobelR = row_pixelR/24 + cols_pixelR/24;
			sobelG = row_pixelG/24 + cols_pixelG/24;
			sobelB = row_pixelB/24 + cols_pixelB/24;
			//total += sobel;
			
			*(pTemp+0) = sobelR;
			*(pTemp+1) = sobelG;
			*(pTemp+2) = sobelB;
			pTemp += 4;
		}
		//pTemp +=16 ;
		pBufTemp += 16;
	} 
	if(pBuf)free(pBuf);
	return;
	
}


void iPhonePhotoFilter(unsigned char* pSrc,unsigned int width,unsigned int height){
	unsigned int line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char pBuf[height][width*4];
	line_size=width*4;
	
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			pBuf[h][w] = *(pTemp+w+h*line_size);
		}
	}
	iPhonePhotoGaussianFilter((unsigned char*)pBuf,width,height);
	iPhonePhotoGaussianFilter((unsigned char*)pBuf,width,height);
	
	pTemp=pSrc;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			
			if(pBuf[h][w]<=127){
				*(pTemp+w+h*line_size)=pBuf[h][w]*pBuf[h][w]/128;
			}
			else if(pBuf[h][w]>=128){
				*(pTemp+w+h*line_size)=pBuf[h][w]+(255-(pBuf[h][w]))*(pBuf[h][w]-127)/128;
			}
			//*(pTemp+w+h*line_size)=((*(pTemp+w+h*line_size))+(pBuf[h][w]))-255;
			//*(pTemp+w+h*line_size)=( (*(pTemp+w+h*line_size)) *(pBuf2[h][w]) )/255+pBuf[h][w];
			//*(pTemp+w+h*line_size)=((pBuf[h][w])*255)/(255+(pBuf2[h][w]));
		}
	}
	
}
void iPhonePhotoMotionBlurFilter(unsigned char* pSrc,unsigned int width,unsigned int height){
	unsigned int line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;//[height+4][width*4+16];
	int pixelR,pixelG,pixelB;
	int random;
	buf_line_size=width*4+16;
	pBuf=(unsigned char*)malloc((height+4)*buf_line_size);
	line_size=width*4;

	
	
	
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			
			//			pBuf[h][w+8] 
			*(pBuf+h*buf_line_size+w+8)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//			pBuf[h+height+2][w+8]
			*(pBuf+(h+height+2)*buf_line_size+w+8)= *(pTemp+w+(height-2+h)*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			//pBuf[h+2][w] 
			*(pBuf+(h+2)*buf_line_size+w)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			//	pBuf[h+2][w+8+width*4]
			*(pBuf+(h+2)*buf_line_size+w+8+width*4)= *(pTemp+(width-2)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//pBuf[h+2][w+8] 
			*(pBuf+(h+2)*buf_line_size+w+8)= *(pTemp+w+h*line_size);
		}
	}
	
	

	
	
	//pTemp+=line_size*2+8;
	for(int h=0; h<height; h++)
	{
		for(int w=0; w<width; w++)
		{
			
			random=rand()%3;
			
 			pixelR = 0;
			pixelG = 0;
			pixelB = 0;
			// line - 1

			pBufTemp += 4;
	
			pBufTemp += 4;


			pBufTemp += 4;

			pBufTemp += 4;

			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 2

		  	pBufTemp += 4;

			pBufTemp += 4;

			pBufTemp += 4;


			pBufTemp += 4;

			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 3

		  	pBufTemp += 4;

			pBufTemp += 4;
			if(random==0){
				pixelR += *pBufTemp;
				pixelG += *(pBufTemp+1);
				pixelB += *(pBufTemp+2);
			}
			pBufTemp += 4;

			pBufTemp += 4;

			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 4

		  	pBufTemp += 4;
			if(random==1){
				pixelR += *pBufTemp;
				pixelG += *(pBufTemp+1);
				pixelB += *(pBufTemp+2);
			}
			pBufTemp += 4;

			pBufTemp += 4;

			pBufTemp += 4;

			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 5
			if(random==2){
				pixelR += *pBufTemp;
				pixelG += *(pBufTemp+1);
				pixelB += *(pBufTemp+2);
			}
		  	pBufTemp += 4;

			pBufTemp += 4;

			pBufTemp += 4;

			pBufTemp += 4;

			
			
			
			
			pBufTemp = pBufTemp - (buf_line_size << 2) - 12;
			
			
			if(pixelR<0) pixelR *= (-1);
			if(pixelG<0) pixelG *= (-1);
			if(pixelB<0) pixelB *= (-1);
			
			//total += sobel;
			
			*(pTemp+0) = pixelR;
			*(pTemp+1) = pixelG;
			*(pTemp+2) = pixelB;
			pTemp += 4;
		}
		//pTemp +=16 ;
		pBufTemp += 16;
	} 
	if(pBuf!=NULL)free(pBuf);
	return;
}
void iPhonePhotoHDRFilter(unsigned char* pSrc,unsigned int width,unsigned int height){
	int buf_size;
	
	unsigned char* p_temp;
	p_temp=pSrc;
	buf_size=width*height;
	for(int i=0;i<buf_size;i++,p_temp+=4){
		if(*p_temp>=128)*p_temp/=2;
		else *p_temp=0;
		if(*(p_temp+1)>=128)*(p_temp+1)/=2;
		else *(p_temp+1)=0;
		if(*(p_temp+2)>=128)*(p_temp+2)/=2;
		else *(p_temp+2)=0;
		/*
		rgbSum=*p_temp+*(p_temp+1)+*(p_temp+2);
		*p_temp=*p_temp*255/(rgbSum+1);
		*(p_temp+1)=*(p_temp+1)*255/(rgbSum+1);
		*(p_temp+2)=*(p_temp+2)*255/(rgbSum+1);
	 */
		
	}
}
void iPhonePhotoHDRFilter1(unsigned char* pSrc,unsigned int width,unsigned int height){
	int buf_size;
	
	unsigned char* p_temp;
	p_temp=pSrc;
	buf_size=width*height;
	for(int i=0;i<buf_size;i++,p_temp+=4){
		if(*p_temp<128)*p_temp*=2;
		else *p_temp=255;
		if(*(p_temp+1)<128)*(p_temp+1)*=2;
		else *(p_temp+1)=255;
		if(*(p_temp+2)<128)*(p_temp+2)*=2;
		else *(p_temp+2)=255;
		/*
		 rgbSum=*p_temp+*(p_temp+1)+*(p_temp+2);
		 *p_temp=*p_temp*255/(rgbSum+1);
		 *(p_temp+1)=*(p_temp+1)*255/(rgbSum+1);
		 *(p_temp+2)=*(p_temp+2)*255/(rgbSum+1);
		 */
		
	}
}
void iPhonePhotoBrightFilterByHDR(unsigned char* pSrc,unsigned int width,unsigned int height){
	int buf_size;
	int x;
	x=200;
	unsigned char* p_temp;
	p_temp=pSrc;
	buf_size=width*height;
	
	for(int i=0;i<buf_size;i++,p_temp+=4){
		
		
		if((*p_temp)*x/100<255) *p_temp=(*p_temp)*x/100;
		else *p_temp=255;
		if((*(p_temp+1))*x/100<255)*(p_temp+1)=(*(p_temp+1))*x/100;
		else *(p_temp+1)=255;
		if((*(p_temp+2))*x/100<255)*(p_temp+2)=(*(p_temp+2))*x/100;
		else *(p_temp+2)=255;
		
		
		
		
	}

}
void iPhonePhotoDarkenFilterByHDR(unsigned char* pSrc,unsigned int width,unsigned int height){
	int buf_size;
	int x;
	x=75;
	unsigned char* p_temp;
	p_temp=pSrc;

	buf_size=width*height;
	
	for(int i=0;i<buf_size;i++,p_temp+=4){
		
		
		 *p_temp=(*p_temp)*x/100;

		*(p_temp+1)=(*(p_temp+1))*x/100;
		
		*(p_temp+2)=(*(p_temp+2))*x/100;
		
		
		
		
		
	}
	
}
void iPhonePhotoFilterLV05(unsigned char* pSrc,unsigned int width,unsigned int height,unsigned int value){
	
	unsigned int line_size;
	unsigned int cnt,temp;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;
	unsigned short a ;
	unsigned short H,W;
	unsigned int* GroupR;
	unsigned int* GroupG;
	unsigned int* GroupB;
	unsigned int* pixNum;
	unsigned short* Group;
	unsigned short* GroupTemp;
	unsigned short* chkTemp;
	

	
	W=width;
	H=height;
	line_size=W*4;
	a=width*sizeof(unsigned short);
	pTemp=pSrc;
	pBuf=(unsigned char*)malloc((height)*(line_size));
	
	Group=(unsigned short*)malloc(H*a);
	GroupTemp=(unsigned short*) Group;
	pBufTemp=(unsigned char*) pBuf;
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			*(pBufTemp+w+h*line_size) = *(pTemp+w+h*line_size);
		}
	}
		
	iPhonePhotoGrayFilter1((unsigned char*)pBuf,width,height,50);
	iPhonePhoto8bitColorG((unsigned char*)pBuf,width,height);
	iPhonePhotoMedianFilter5x5G((unsigned char*)pBuf,width,height);
	temp=0;
///////그룹화/////////////////////////////////
	///////그룹화////////////////////////////////
	///////그룹화////////////////////////////////
	///////그룹화////////////////////////////////
	///////그룹화////////////////////////////////
	for(int h=0; h<(H); h++)
	{
		for(int w=0; w<(W); w++)
		{
			//pBuf[h+2][w+8] 
			*(Group+(h)*width+w)= 0;
		}
	}
	
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	for(int h=0; h<(H); h++)
	{
		for(int w=0; w<(W); w++)
		{
			if(h>0&&(*(pBufTemp)==*(pBufTemp-line_size))){
				*(GroupTemp)=*(GroupTemp-W);
				
				
				 if(w>0&&(*(pBufTemp)==*(pBufTemp-4))&&*(GroupTemp)!=*(GroupTemp-1)){
					 for(int i=0; i<=h; i++){
						for(int j=0; j<W; j++){
							if(*(Group+i*W+j)==*(GroupTemp-1)) *(Group+i*W+j)=*(GroupTemp);
						}
					 }
				 }
				 
				
			}
			else if(w>0&&(*(pBufTemp)==*(pBufTemp-4)))*(GroupTemp)=*(GroupTemp-1);
			
			else if(h>0&&w<(width-1)&&(*(pBufTemp)==*(pBufTemp-line_size+4))){
				*(GroupTemp)=*(GroupTemp-W+1);
				
			}
			
			else *(GroupTemp)=temp++;
			pBufTemp+=4;
			GroupTemp++;
			
		}
	}
				
	pTemp=pSrc;
	GroupTemp=(unsigned short*) Group;
	chkTemp=(unsigned short*)malloc(sizeof(unsigned short)*temp);
	//chkTemp1=(unsigned short*)malloc(sizeof(unsigned short)*temp);
	//if(w>0&&(*(pTemp)==*(pTemp-4))&&*(pBufTemp)!=*(pBufTemp-1))
	for(int x=0;x<temp;x++)*(chkTemp+x)=0;
	for(int h=0; h<(H); h++)
	{
		for(int w=0; w<(W); w++){
			*(chkTemp+*(GroupTemp))+=1;
			GroupTemp++;
		}
	}
	cnt=0;
	for(int x=0;x<temp;x++)if(*(chkTemp+x)>0)*(chkTemp+x)=cnt++;
	GroupTemp=(unsigned short*) Group;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width); w++)
		{
			*(GroupTemp)=*(chkTemp+*(GroupTemp));
			GroupTemp++;
		}
	}
	
	
	
	////

	

	GroupTemp=(unsigned short*) Group;
	/*
	for(int x=0;x<temp;x++)chkTemp1[x]=0;
	 
	
	for(int h=0; h<(H); h++)
	{
		for(int w=0; w<(W); w++){
			if(w>0&&(*(pTemp)==*(pTemp-4))&&*(pBufTemp)!=*(pBufTemp-1)){
				if(chkTemp1[*(GroupTemp-1)]==0||chkTemp1[*(GroupTemp-1)]>*(GroupTemp))chkTemp1[*(GroupTemp-1)]=*(GroupTemp);
				
				//else if(chkTemp1[*(pBufTemp-1)]==0)chkTemp1[*(pBufTemp-1)]=*(pBufTemp);
				
				//(chkTemp1+*(pBufTemp-1))=*(pBufTemp);
				//NSLog(@"%d",)
			}
			
			
			GroupTemp++;
			pTemp+=4;
			
			
		}
	}
	 */
//	unsigned short tt[temp];
//	for(int i=1;i<temp;i++){tt[i]=chkTemp1[i];}
	//
	

	/*
	for(int i=1;i<temp;i++){
		if(chkTemp1[i]){
			index = i;
			count = 0;
			do{
				count++;
				index = chkTemp1[index];
			}while(chkTemp1[index]);
			chkTemp1[i] = index;
		}
	}*/
	
	
	
	pTemp=pSrc;
	GroupTemp=(unsigned short*) Group;
	
	for(int h=0; h<(H); h++)
	{
		for(int w=0; w<(W); w++){
			//if(chkTemp1[*(GroupTemp-1)])*(GroupTemp)=chkTemp1[*(GroupTemp)];
			
			GroupTemp++;
			
			
		}
	}
	 
	
	////////////////그룹화////////////////////////////////
		////////////////그룹화////////////////////////////////
		////////////////그룹화////////////////////////////////
		////////////////그룹화////////////////////////////////

	pTemp=pSrc;
	GroupTemp=(unsigned short*) Group;
	GroupR=(unsigned int*)malloc(sizeof(unsigned int)*cnt);
	GroupG=(unsigned int*)malloc(sizeof(unsigned int)*cnt);
	GroupB=(unsigned int*)malloc(sizeof(unsigned int)*cnt);
	pixNum=(unsigned int*)malloc(sizeof(unsigned int)*cnt);
	
	for(short x=0;x<cnt;x++){
		GroupR[x]=0;
		GroupG[x]=0;
		GroupB[x]=0;
		pixNum[x]=0;
	}
	
	for(int h=0; h<(H); h++)
	{
		for(int w=0; w<(W); w++)
		{
			
			
			GroupR[*(Group+h*W+w)]+=*(pTemp+h*line_size+w*4);
			GroupG[*(Group+h*W+w)]+=*(pTemp+h*line_size+w*4+1);
			GroupB[*(Group+h*W+w)]+=*(pTemp+h*line_size+w*4+2);
			pixNum[*(Group+h*W+w)]++;
			
		}
	}
	for(short x=0;x<cnt;x++){
		if(pixNum[x]){
			GroupR[x]/=pixNum[x];
			GroupG[x]/=pixNum[x];
			GroupB[x]/=pixNum[x];
		}
	}
	for(int h=0; h<(H); h++)
	{
		for(int w=0; w<(W); w++)
		{
			*(pTemp+h*line_size+w*4)=GroupR[*(Group+h*W+w)];
			*(pTemp+h*line_size+w*4+1)=GroupG[*(Group+h*W+w)];
			*(pTemp+h*line_size+w*4+2)=GroupB[*(Group+h*W+w)];
			
		}
	}


	
/*
	for(int h=0; h<(H); h++)
	{
		for(int w=0; w<(W); w++)
		{
							
				*(pTemp+h*line_size+w*4)=*(Group+h*W+w);
				*(pTemp+h*line_size+w*4+1)=*(Group+h*W+w);
				*(pTemp+h*line_size+w*4+2)=*(Group+h*W+w);
				
		}
	}
 */
	if(Group!=NULL)free(Group);
	if(pBuf!=NULL)free(pBuf);
	if(GroupR!=NULL)free(GroupR);
	if(GroupG!=NULL)free(GroupG);
					
	if(GroupB!=NULL)free(GroupB);
	if(pixNum!=NULL)free(pixNum);

	if(chkTemp!=NULL)free(chkTemp);


}

void iPhonePhotoFilterLV05_2(unsigned char* pSrc,unsigned int width,unsigned int height){
	
	unsigned int line_size;
	unsigned int cnt,temp;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;
	unsigned short a ;
	unsigned short H,W;
	unsigned int* GroupR;
	unsigned int* GroupG;
	unsigned int* GroupB;
	unsigned int* pixNum;
	unsigned short* Group;
	unsigned short* GroupTemp;
	unsigned short* chkTemp;
	
	
	
	W=width;
	H=height;
	line_size=W*4;
	a=width*sizeof(unsigned short);
	pTemp=pSrc;
	pBuf=(unsigned char*)malloc((height)*(line_size));
	
	Group=(unsigned short*)malloc(H*a);
	GroupTemp=(unsigned short*) Group;
	pBufTemp=(unsigned char*) pBuf;
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			*(pBufTemp+w+h*line_size) = *(pTemp+w+h*line_size);
		}
	}
	
	iPhonePhotoGrayFilter1((unsigned char*)pBuf,width,height,50);
	iPhonePhoto8bitColorG((unsigned char*)pBuf,width,height);
	//iPhonePhotoMedianFilterG((unsigned char*)pBuf,width,height);
	temp=0;
	///////그룹화/////////////////////////////////
	///////그룹화////////////////////////////////
	///////그룹화////////////////////////////////
	///////그룹화////////////////////////////////
	///////그룹화////////////////////////////////
	for(int h=0; h<(H); h++)
	{
		for(int w=0; w<(W); w++)
		{
			//pBuf[h+2][w+8] 
			*(Group+(h)*width+w)= 0;
		}
	}
	
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	for(int h=0; h<(H); h++)
	{
		for(int w=0; w<(W); w++)
		{
			if(h>0&&(*(pBufTemp)==*(pBufTemp-line_size))){
				*(GroupTemp)=*(GroupTemp-W);
				
				
				if(w>0&&(*(pBufTemp)==*(pBufTemp-4))&&*(GroupTemp)!=*(GroupTemp-1)){
					for(int i=0; i<=h; i++){
						for(int j=0; j<W; j++){
							if(*(Group+i*W+j)==*(GroupTemp-1)) *(Group+i*W+j)=*(GroupTemp);
						}
					}
				}
				
				
			}
			else if(w>0&&(*(pBufTemp)==*(pBufTemp-4)))*(GroupTemp)=*(GroupTemp-1);
			
			else if(h>0&&w<(width-1)&&(*(pBufTemp)==*(pBufTemp-line_size+4))){
				*(GroupTemp)=*(GroupTemp-W+1);
				
			}
			
			else *(GroupTemp)=temp++;
			pBufTemp+=4;
			GroupTemp++;
			
		}
	}
	
	pTemp=pSrc;
	GroupTemp=(unsigned short*) Group;
	chkTemp=(unsigned short*)malloc(sizeof(unsigned short)*temp);
	//chkTemp1=(unsigned short*)malloc(sizeof(unsigned short)*temp);
	//if(w>0&&(*(pTemp)==*(pTemp-4))&&*(pBufTemp)!=*(pBufTemp-1))
	for(int x=0;x<temp;x++)*(chkTemp+x)=0;
	for(int h=0; h<(H); h++)
	{
		for(int w=0; w<(W); w++){
			*(chkTemp+*(GroupTemp))+=1;
			GroupTemp++;
		}
	}
	cnt=0;
	for(int x=0;x<temp;x++)if(*(chkTemp+x)>0)*(chkTemp+x)=cnt++;
	GroupTemp=(unsigned short*) Group;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width); w++)
		{
			*(GroupTemp)=*(chkTemp+*(GroupTemp));
			GroupTemp++;
		}
	}
	
	
	
	////
	
	
	
	GroupTemp=(unsigned short*) Group;
	/*
	 for(int x=0;x<temp;x++)chkTemp1[x]=0;
	 
	 
	 for(int h=0; h<(H); h++)
	 {
	 for(int w=0; w<(W); w++){
	 if(w>0&&(*(pTemp)==*(pTemp-4))&&*(pBufTemp)!=*(pBufTemp-1)){
	 if(chkTemp1[*(GroupTemp-1)]==0||chkTemp1[*(GroupTemp-1)]>*(GroupTemp))chkTemp1[*(GroupTemp-1)]=*(GroupTemp);
	 
	 //else if(chkTemp1[*(pBufTemp-1)]==0)chkTemp1[*(pBufTemp-1)]=*(pBufTemp);
	 
	 //(chkTemp1+*(pBufTemp-1))=*(pBufTemp);
	 //NSLog(@"%d",)
	 }
	 
	 
	 GroupTemp++;
	 pTemp+=4;
	 
	 
	 }
	 }
	 */
	//	unsigned short tt[temp];
	//	for(int i=1;i<temp;i++){tt[i]=chkTemp1[i];}
	//
	
	
	
	/*
	 for(int i=1;i<temp;i++){
	 if(chkTemp1[i]){
	 index = i;
	 count = 0;
	 do{
	 count++;
	 index = chkTemp1[index];
	 }while(chkTemp1[index]);
	 chkTemp1[i] = index;
	 }
	 }*/
	
	
	
	pTemp=pSrc;
	GroupTemp=(unsigned short*) Group;
	
	for(int h=0; h<(H); h++)
	{
		for(int w=0; w<(W); w++){
			//if(chkTemp1[*(GroupTemp-1)])*(GroupTemp)=chkTemp1[*(GroupTemp)];
			
			GroupTemp++;
			
			
		}
	}
	
	
	////////////////그룹화////////////////////////////////
	////////////////그룹화////////////////////////////////
	////////////////그룹화////////////////////////////////
	////////////////그룹화////////////////////////////////
	
	pTemp=pSrc;
	GroupTemp=(unsigned short*) Group;
	GroupR=(unsigned int*)malloc(sizeof(unsigned int)*cnt);
	GroupG=(unsigned int*)malloc(sizeof(unsigned int)*cnt);
	GroupB=(unsigned int*)malloc(sizeof(unsigned int)*cnt);
	pixNum=(unsigned int*)malloc(sizeof(unsigned int)*cnt);
	
	for(short x=0;x<cnt;x++){
		GroupR[x]=0;
		GroupG[x]=0;
		GroupB[x]=0;
		pixNum[x]=0;
	}
	
	for(int h=0; h<(H); h++)
	{
		for(int w=0; w<(W); w++)
		{
			
			
			GroupR[*(Group+h*W+w)]+=*(pTemp+h*line_size+w*4);
			GroupG[*(Group+h*W+w)]+=*(pTemp+h*line_size+w*4+1);
			GroupB[*(Group+h*W+w)]+=*(pTemp+h*line_size+w*4+2);
			pixNum[*(Group+h*W+w)]++;
			
		}
	}
	for(short x=0;x<cnt;x++){
		if(pixNum[x]){
			GroupR[x]/=pixNum[x];
			GroupG[x]/=pixNum[x];
			GroupB[x]/=pixNum[x];
		}
	}
	for(int h=0; h<(H); h++)
	{
		for(int w=0; w<(W); w++)
		{
			*(pTemp+h*line_size+w*4)=GroupR[*(Group+h*W+w)];
			*(pTemp+h*line_size+w*4+1)=GroupG[*(Group+h*W+w)];
			*(pTemp+h*line_size+w*4+2)=GroupB[*(Group+h*W+w)];
			
		}
	}
	
	
	
	/*
	 for(int h=0; h<(H); h++)
	 {
	 for(int w=0; w<(W); w++)
	 {
	 
	 *(pTemp+h*line_size+w*4)=*(Group+h*W+w);
	 *(pTemp+h*line_size+w*4+1)=*(Group+h*W+w);
	 *(pTemp+h*line_size+w*4+2)=*(Group+h*W+w);
	 
	 }
	 }
	 */
	if(Group!=NULL)free(Group);
	if(pBuf!=NULL)free(pBuf);
	if(GroupR!=NULL)free(GroupR);
	if(GroupG!=NULL)free(GroupG);
	
	if(GroupB!=NULL)free(GroupB);
	if(pixNum!=NULL)free(pixNum);
	
	if(chkTemp!=NULL)free(chkTemp);
	
}
void iPhonePhotoFilterLV05_3(unsigned char* pSrc,unsigned int width,unsigned int height){
	unsigned int line_size, temp;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char pBuf[height][width*4];
	unsigned char pBuf2[height][width*4];
	unsigned char pBuf3[height][width*4];
	
	line_size=width*4;
	
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			pBuf[h][w] = *(pTemp+w+h*line_size);
		}
	}
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			pBuf2[h][w] = *(pTemp+w+h*line_size);
		}
	}
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			pBuf3[h][w] = *(pTemp+w+h*line_size);
		}
	}
	iPhonePhotoBrightFilterByHDR((unsigned char*)pBuf2,width,height);
	iPhonePhotoGrayFilter1((unsigned char*)pBuf,width,height,50);
	iPhonePhotoBiningFilter2((unsigned char*)pBuf3,width,height);
	//iPhonePhotoInvertFilter((unsigned char*)pBuf2,width,height);//	pBuf[h][w]
	

	pTemp=pSrc;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			if(pBuf[h][w]<4)temp=((unsigned int)*(pTemp+w+h*line_size))*2;
			else if(pBuf[h][w]<8)temp=(((unsigned int)*(pTemp+w+h*line_size))*2-4);
			else if(pBuf[h][w]<16)temp=(((unsigned int)*(pTemp+w+h*line_size))*2-8);
			else if(pBuf[h][w]<32)temp=(((unsigned int)*(pTemp+w+h*line_size))*2-16);
			else if(pBuf[h][w]<64)temp=(((unsigned int)*(pTemp+w+h*line_size))*2-32);
			else if(pBuf[h][w]<96)temp=(((unsigned int)*(pTemp+w+h*line_size))*2-64);
			else if(pBuf[h][w]<128)temp=(((unsigned int)*(pTemp+w+h*line_size))*2-96);
			else if(pBuf[h][w]<160)temp=(((unsigned int)*(pTemp+w+h*line_size))*2-160);
			else if(pBuf[h][w]<192)temp=(((unsigned int)*(pTemp+w+h*line_size))*2-192);
			else if(pBuf[h][w]<224)temp=(((unsigned int)*(pTemp+w+h*line_size))*2-224);
			//else if(pBuf[h][w]<=255)temp=(((unsigned int)*(pTemp+w+h*line_size))*2-254);
			
			if(temp>255)temp=255;
			else if(temp<0)temp=0;
			
			temp=((pBuf2[h][w])*temp)/255;
			
		//	*(pTemp+w+h*line_size)=pBuf2[h][w];
			*(pTemp+w+h*line_size)=temp;
			//if(pBuf[h][w]<50)*(pTemp+w+h*line_size)=pBuf3[h][w];
			/*
			if(pBuf[h][w]<=total){
					if(*(pTemp+w+h*line_size)*(255-pBuf[h][w])/total<255)*(pTemp+w+h*line_size)=*(pTemp+w+h*line_size)*(255-pBuf[h][w])/total;
				else *(pTemp+w+h*line_size)=255;
				
					}
			else if(pBuf[h][w]>=total)
			{
				*(pTemp+w+h*line_size)=((int)*(pTemp+w+h*line_size))*(500+total-pBuf[h][w])/500;
			}
			 */
		
		}
	}
	
//	iPhonePhotoOverlayFilter(pSrc,width,height);
	//	iPhonePhotoOverlayFilter(pSrc,width,height);
	//iPhonePhotoOverlayFilter(pSrc,width,height);
	
}


unsigned char* iPhonePhotoThumbBottonFilter(unsigned char* pSrc,unsigned int width,unsigned int height){
	
	unsigned int line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;

	line_size=width*4;
	buf_line_size=width*4+16;
	pBuf=(unsigned char*)malloc((height+4)*(width*4+16));
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=pBuf;
	//pBufTemp=(unsigned char*) pBuf;
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width)*4+16; w++)
		{
			*(pBufTemp+h*buf_line_size+w) = 255;

		}
	}
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width)*4+16; w++)
		{
			*(pBufTemp+(h+height+2)*buf_line_size+w) = 255;

		}
	}
	
	for(int h=0; h<(height)+4; h++)
	{
		for(int w=0; w<8;w++)
		{
			*(pBufTemp+h*buf_line_size+w) = 255;

		}
	}
	
	for(int h=0; h<(height)+4; h++)
	{
		for(int w=0; w<8; w++)
		{
			//pBuf[h+2][w+8+width*4] = *(pTemp+(width-2)*4+w+h*line_size);
			*(pBufTemp+h*buf_line_size+w+(width+2)*4) = 255;

		}
	}
	pBufTemp=pBuf+buf_line_size*2;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			*(pBufTemp+h*buf_line_size+w+8) = *(pTemp+w+h*line_size);
		}
	}
	
	free(pSrc);
	//pTemp+=line_size*2+8;
	return pBuf;
	
}

void iPhonePhotoThumbClickedFilter(unsigned char* pSrc,unsigned int width,unsigned int height){
	unsigned int line_size;
	unsigned char* pTemp;


	line_size=width*4;
	
	//	buf_size=height*line_size;
	pTemp=pSrc;

	
	

	//iPhonePhotoGrayFilter1((unsigned char*)pBuf,width,height);
	
	pTemp=pSrc;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			*(pTemp+w+h*line_size)=*(pTemp+w+h*line_size)*80/255;
			


		}
	}
}
void iPhonePhotoTestLoMoFilter(unsigned char* pSrc,unsigned int width,unsigned int height){
	int buf_size;
	
	unsigned char* p_temp;
	int rgbSum,x,y,sumX,sumY;

	p_temp=pSrc;
	buf_size=width*height;
	for(int h=0;h<height;h++){
		for(int w=0;w<width;w++){
		
			x=height/2-h;
			y=width/2-w;
			if(height/2<h)
			{
				x*=(-1);
			}
			if(width/2<w)
			{
				y*=(-1);   
			}

			sumY=300-y*255/(width/2);
			sumX=300-x*255/(height/2);
			if(sumY>255)sumY=255;
			if(sumX>255)sumX=255;
			if(sumY<100)sumY=sumY-(100-sumY);
			if(sumX<100)sumX=sumX-(100-sumX);
			if(sumY<0)sumY=0;
			if(sumX<0)sumX=0;
			rgbSum=(sumX+sumY)/2;

			
			*p_temp=rgbSum;
			
			*(p_temp+1)=rgbSum;
			*(p_temp+2)=rgbSum;

			p_temp+=4;
		}
		
	}
}

void iPhonePhotoFilterLV04(unsigned char* pSrc,unsigned int width,unsigned int height){
	unsigned int line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char *pBuf;
	unsigned char *pBuf2;
	line_size=width*4;
	
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBuf=(unsigned char*)malloc(line_size*height);
	pBuf2=(unsigned char*)malloc(line_size*height);
	pBufTemp=(unsigned char*) pBuf;
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			*(pBuf+w+h*line_size) = *(pTemp+w+h*line_size);
		}
	}
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			*(pBuf2+w+h*line_size) = *(pTemp+w+h*line_size);
		}
	}
	iPhonePhotoTestLoMoFilter((unsigned char*)pBuf,width,height);
	iPhonePhotoToneFilter((unsigned char*)pBuf,width,height, 255, 235,235);

	
	pTemp=pSrc;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			*(pTemp+w+h*line_size)=*(pTemp+w+h*line_size)*(*(pBuf+w+h*line_size))/255;
			
			if(*(pTemp+w+h*line_size)<=127){
				*(pTemp+w+h*line_size)=*(pTemp+w+h*line_size)*(*(pBuf2+w+h*line_size))/128;
			}
			else if(*(pTemp+w+h*line_size)>=128){
				*(pTemp+w+h*line_size)=*(pTemp+w+h*line_size)+(255-*(pTemp+w+h*line_size))*(*(pBuf2+w+h*line_size)-127)/128;
			}

	
		}
	}
	if(pBuf!=NULL)free(pBuf);
	if(pBuf2!=NULL)free(pBuf2);
	
}

void iPhonePhotoBiningFilter3(unsigned char* pSrc,unsigned int width,unsigned int height){
	
	unsigned int buf_size,line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char pBuf[height+2][width*4+8];
	int pixelR,pixelG,pixelB;
	line_size=width*4;
	buf_line_size=width*4+8;
	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	for(int w=0; w<(width*4); w++)
	{
		pBuf[0][w+4] =*(pTemp+w);
	}
	
	
	for(int w=0; w<(width*4); w++)
	{
		pBuf[height+1][w+4] =*(pTemp+w+(height-1)*line_size);
	}
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<4; w++)
		{
			pBuf[h+1][w] = *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<4; w++)
		{
			pBuf[h+1][w+4+width*4] =*(pTemp+(width-1)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			pBuf[h+1][w+4] = *(pTemp+w+h*line_size);
		}
	}
	
	
	
	
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width); w++)
		{
 			pixelR = 0;
			pixelG = 0;
			pixelB = 0;
			// line - 1
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 2
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 3
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp - (buf_line_size << 1) - 4;
			
			pixelR/=12;
			pixelG/=12;
			pixelB/=12;
			if(pixelR>255) pixelR = 255;
			if(pixelG>255) pixelG = 255;
			if(pixelB>255) pixelB = 255;
			

			//total += sobel;
			
			*(pTemp+0) = pixelR;
			*(pTemp+1) = pixelG;
			*(pTemp+2) = pixelB;
			pTemp += 4;
		}
		pBufTemp += 8;
	} 
	
	
	return;
	
}
void iPhonePhotoBiningFilter2(unsigned char* pSrc,unsigned int width,unsigned int height){
	
	unsigned int buf_size,line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char pBuf[height+2][width*4+8];
	int pixelR,pixelG,pixelB;
	line_size=width*4;
	buf_line_size=width*4+8;
	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	for(int w=0; w<(width*4); w++)
	{
		pBuf[0][w+4] =*(pTemp+w);
	}
	
	
	for(int w=0; w<(width*4); w++)
	{
		pBuf[height+1][w+4] =*(pTemp+w+(height-1)*line_size);
	}
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<4; w++)
		{
			pBuf[h+1][w] = *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<4; w++)
		{
			pBuf[h+1][w+4+width*4] =*(pTemp+(width-1)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			pBuf[h+1][w+4] = *(pTemp+w+h*line_size);
		}
	}
	
	
	
	
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width); w++)
		{
 			pixelR = 0;
			pixelG = 0;
			pixelB = 0;
			// line - 1

		  	pBufTemp += 4;
	
			pBufTemp += 4;

			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 2

		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 3

		  	pBufTemp += 4;
		 	pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += *pBufTemp;
			pixelG += *(pBufTemp+1);
			pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp - (buf_line_size << 1) - 4;
			
			
			if(pixelR>255) pixelR = 255;
			if(pixelG>255) pixelG = 255;
			if(pixelB>255) pixelB = 255;
			
			
			//total += sobel;
			
			*(pTemp+0) = pixelR;
			*(pTemp+1) = pixelG;
			*(pTemp+2) = pixelB;
			pTemp += 4;
		}
		pBufTemp += 8;
	} 
	
	
	return;
	
}
void iPhonePhoto8bitColorG(unsigned char* pSrc,unsigned int width,unsigned int height){
	int buf_size;
	unsigned char* p_temp;
	
	p_temp=pSrc;
	buf_size=width*height;
	for(int i=0;i<buf_size;i++,p_temp+=4){
		*p_temp=(*p_temp>>6);
		*p_temp=(*p_temp<<6);
		
	}
	
}
void iPhonePhoto8bitColor(unsigned char* pSrc,unsigned int width,unsigned int height){
	int buf_size;
unsigned char* p_temp;

	p_temp=pSrc;
	buf_size=width*height;
	for(int i=0;i<buf_size;i++,p_temp+=4){
		*p_temp=(*p_temp>>6);
		*p_temp=(*p_temp<<6);
		
		*(p_temp+1)=(*(p_temp+1)>>6);
		*(p_temp+1)=(*(p_temp+1)<<6);
		
		*(p_temp+2)=(*(p_temp+2)>>6);
		*(p_temp+2)=(*(p_temp+2)<<6);
	


		
	}
	
}
void iPhonePhoto16bitColor(unsigned char* pSrc,unsigned int width,unsigned int height){
	int buf_size;
	unsigned char* p_temp;
	
	p_temp=pSrc;
	buf_size=width*height;
	for(int i=0;i<buf_size;i++,p_temp+=4){
		*p_temp-=(*p_temp%32);
		//*p_temp+=31;
		*(p_temp+1)-=(*(p_temp+1)%32);
		//*(p_temp+1)+=31;
		*(p_temp+2)-=(*(p_temp+2)%32);
		//*(p_temp+2)+=31;
		
	}
	
}
/*

iPhonePhotoHistoEqual(unsigned char* pSrc,unsigned int width,unsigned int height){
	
//	int i,j;
	unsigned int histogramR[256];
	unsigned int histogramG[256];
	unsigned int histogramB[256];
	unsigned int sumR_hist[256];
	unsigned int sumG_hist[256];
	unsigned int sumB_hist[256];
	unsigned char* p_temp;
	p_temp=pSrc;
	for(int i;i<256;i++){
		histogramR[i]=0;
		histogramG[i]=0;
		histogramB[i]=0;
	}
	
	for(int i=0;i<height;i++)
	{
		for(int j=0;j<width;j++)
		{
			
			histogramR[*(p_temp+0)]++;
			histogramG[*(p_temp+1)]++;
			histogramB[*(p_temp+2)]++;
			p_temp+=4;
			
		}
	}
	int sumR =0;
	int sumG =0;
	int sumB =0;
	float scale_factor=255.0f/(float)(height*width);
	
	for(int i=0;i<256;i++){
		sumR +=histogramR[i];
		sumG +=histogramG[i];
		sumB +=histogramB[i];
		
	}
	
			
	
}
*/
void iPhonePhotoMedianFilter9x9(unsigned char* pSrc,unsigned int width,unsigned int height){
	int l,k;
	unsigned int buf_size,line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char pBuf[width*4][height];
	unsigned char pixelR[81],pixelG[81],pixelB[81],temp;
	line_size=width*4;
	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	for(int i=0; i<buf_size; i++)
	{
		*(pBufTemp+i) = *(pTemp+i);
	}
	
	
	
	pTemp+=line_size*4+4*4;
	for(int h=4; h<(height-4); h++)
	{
		for(int w=4; w<(width-4); w++)
		{
			// line - 1
			pixelR[0] = *pBufTemp;
			pixelG[0] = *(pBufTemp+1);
			pixelB[0] += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[1] = *pBufTemp;
			pixelG[1] = *(pBufTemp+1);
			pixelB[1] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[2] = *pBufTemp;
			pixelG[2] = *(pBufTemp+1);
			pixelB[2] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[3] = *pBufTemp;
			pixelG[3] = *(pBufTemp+1);
			pixelB[3] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[4] = *pBufTemp;
			pixelG[4] = *(pBufTemp+1);
			pixelB[4] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[5] = *pBufTemp;
			pixelG[5] = *(pBufTemp+1);
			pixelB[5] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[6] = *pBufTemp;
			pixelG[6] = *(pBufTemp+1);
			pixelB[6] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[7] = *pBufTemp;
			pixelG[7] = *(pBufTemp+1);
			pixelB[7] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[8] = *pBufTemp;
			pixelG[8] = *(pBufTemp+1);
			pixelB[8] = *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 32;
			// line - 1
			pixelR[9] = *pBufTemp;
			pixelG[9] = *(pBufTemp+1);
			pixelB[9] += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[10] = *pBufTemp;
			pixelG[10] = *(pBufTemp+1);
			pixelB[10] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[11] = *pBufTemp;
			pixelG[11] = *(pBufTemp+1);
			pixelB[11] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[12] = *pBufTemp;
			pixelG[12] = *(pBufTemp+1);
			pixelB[12] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[13] = *pBufTemp;
			pixelG[13] = *(pBufTemp+1);
			pixelB[13] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[14] = *pBufTemp;
			pixelG[14] = *(pBufTemp+1);
			pixelB[14] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[15] = *pBufTemp;
			pixelG[15] = *(pBufTemp+1);
			pixelB[15] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[16] = *pBufTemp;
			pixelG[16] = *(pBufTemp+1);
			pixelB[16] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[17] = *pBufTemp;
			pixelG[17] = *(pBufTemp+1);
			pixelB[17] = *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 32;
			pixelR[18] = *pBufTemp;
			pixelG[18] = *(pBufTemp+1);
			pixelB[18] += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[19] = *pBufTemp;
			pixelG[19] = *(pBufTemp+1);
			pixelB[19] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[20] = *pBufTemp;
			pixelG[20] = *(pBufTemp+1);
			pixelB[20] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[21] = *pBufTemp;
			pixelG[21] = *(pBufTemp+1);
			pixelB[21] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[22] = *pBufTemp;
			pixelG[22] = *(pBufTemp+1);
			pixelB[22] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[23] = *pBufTemp;
			pixelG[23] = *(pBufTemp+1);
			pixelB[23] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[24] = *pBufTemp;
			pixelG[24] = *(pBufTemp+1);
			pixelB[24] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[25] = *pBufTemp;
			pixelG[25] = *(pBufTemp+1);
			pixelB[25] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[26] = *pBufTemp;
			pixelG[26] = *(pBufTemp+1);
			pixelB[26] = *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 32;
			pixelR[27] = *pBufTemp;
			pixelG[27] = *(pBufTemp+1);
			pixelB[27] += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[28] = *pBufTemp;
			pixelG[28] = *(pBufTemp+1);
			pixelB[28] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[29] = *pBufTemp;
			pixelG[29] = *(pBufTemp+1);
			pixelB[29] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[30] = *pBufTemp;
			pixelG[30] = *(pBufTemp+1);
			pixelB[30] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[31] = *pBufTemp;
			pixelG[31] = *(pBufTemp+1);
			pixelB[31] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[32] = *pBufTemp;
			pixelG[32] = *(pBufTemp+1);
			pixelB[32] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[33] = *pBufTemp;
			pixelG[33] = *(pBufTemp+1);
			pixelB[33] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[34] = *pBufTemp;
			pixelG[34] = *(pBufTemp+1);
			pixelB[34] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[35] = *pBufTemp;
			pixelG[35] = *(pBufTemp+1);
			pixelB[35] = *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 32;
			pixelR[36] = *pBufTemp;
			pixelG[36] = *(pBufTemp+1);
			pixelB[36] += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[37] = *pBufTemp;
			pixelG[37] = *(pBufTemp+1);
			pixelB[37] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[38] = *pBufTemp;
			pixelG[38] = *(pBufTemp+1);
			pixelB[38] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[39] = *pBufTemp;
			pixelG[39] = *(pBufTemp+1);
			pixelB[39] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[40] = *pBufTemp;
			pixelG[40] = *(pBufTemp+1);
			pixelB[40] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[41] = *pBufTemp;
			pixelG[41] = *(pBufTemp+1);
			pixelB[41] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[42] = *pBufTemp;
			pixelG[42] = *(pBufTemp+1);
			pixelB[42] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[43] = *pBufTemp;
			pixelG[43] = *(pBufTemp+1);
			pixelB[43] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[44] = *pBufTemp;
			pixelG[44] = *(pBufTemp+1);
			pixelB[44] = *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 32;
			pixelR[45] = *pBufTemp;
			pixelG[45] = *(pBufTemp+1);
			pixelB[45] += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[46] = *pBufTemp;
			pixelG[46] = *(pBufTemp+1);
			pixelB[46] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[47] = *pBufTemp;
			pixelG[47] = *(pBufTemp+1);
			pixelB[47] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[48] = *pBufTemp;
			pixelG[48] = *(pBufTemp+1);
			pixelB[48] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[49] = *pBufTemp;
			pixelG[49] = *(pBufTemp+1);
			pixelB[49] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[50] = *pBufTemp;
			pixelG[50] = *(pBufTemp+1);
			pixelB[50] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[51] = *pBufTemp;
			pixelG[51] = *(pBufTemp+1);
			pixelB[51] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[52] = *pBufTemp;
			pixelG[52] = *(pBufTemp+1);
			pixelB[52] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[53] = *pBufTemp;
			pixelG[53] = *(pBufTemp+1);
			pixelB[53] = *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 32;
			pixelR[54] = *pBufTemp;
			pixelG[54] = *(pBufTemp+1);
			pixelB[54] += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[55] = *pBufTemp;
			pixelG[55] = *(pBufTemp+1);
			pixelB[55] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[56] = *pBufTemp;
			pixelG[56] = *(pBufTemp+1);
			pixelB[56] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[57] = *pBufTemp;
			pixelG[57] = *(pBufTemp+1);
			pixelB[57] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[58] = *pBufTemp;
			pixelG[58] = *(pBufTemp+1);
			pixelB[58] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[59] = *pBufTemp;
			pixelG[59] = *(pBufTemp+1);
			pixelB[59] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[60] = *pBufTemp;
			pixelG[60] = *(pBufTemp+1);
			pixelB[60] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[61] = *pBufTemp;
			pixelG[61] = *(pBufTemp+1);
			pixelB[61] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[62] = *pBufTemp;
			pixelG[62] = *(pBufTemp+1);
			pixelB[62] = *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 32;
			pixelR[63] = *pBufTemp;
			pixelG[63] = *(pBufTemp+1);
			pixelB[63] += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[64] = *pBufTemp;
			pixelG[64] = *(pBufTemp+1);
			pixelB[64] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[65] = *pBufTemp;
			pixelG[65] = *(pBufTemp+1);
			pixelB[65] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[66] = *pBufTemp;
			pixelG[66] = *(pBufTemp+1);
			pixelB[66] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[67] = *pBufTemp;
			pixelG[67] = *(pBufTemp+1);
			pixelB[67] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[68] = *pBufTemp;
			pixelG[68] = *(pBufTemp+1);
			pixelB[68] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[69] = *pBufTemp;
			pixelG[69] = *(pBufTemp+1);
			pixelB[69] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[70] = *pBufTemp;
			pixelG[70] = *(pBufTemp+1);
			pixelB[70] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[71] = *pBufTemp;
			pixelG[71] = *(pBufTemp+1);
			pixelB[71] = *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 32;
			pixelR[72] = *pBufTemp;
			pixelG[72] = *(pBufTemp+1);
			pixelB[72] += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[73] = *pBufTemp;
			pixelG[73] = *(pBufTemp+1);
			pixelB[73] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[74] = *pBufTemp;
			pixelG[74] = *(pBufTemp+1);
			pixelB[74] = *(pBufTemp+2);
			pBufTemp+= 4;
			pixelR[75] = *pBufTemp;
			pixelG[75] = *(pBufTemp+1);
			pixelB[75] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[76] = *pBufTemp;
			pixelG[76] = *(pBufTemp+1);
			pixelB[76] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[77] = *pBufTemp;
			pixelG[77] = *(pBufTemp+1);
			pixelB[77] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[78] = *pBufTemp;
			pixelG[78] = *(pBufTemp+1);
			pixelB[78] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[79] = *pBufTemp;
			pixelG[79] = *(pBufTemp+1);
			pixelB[79] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[80] = *pBufTemp;
			pixelG[80] = *(pBufTemp+1);
			pixelB[80] = *(pBufTemp+2);
						
			pBufTemp = pBufTemp - (line_size << 3) - 28;
			
			
			for(k=0;k<81;k++){
				for(l=0;l<81;l++){
					if(pixelR[l] < pixelR[l+1]) {
                        temp = pixelR[l];
                        pixelR[l] = pixelR[l+1];
                        pixelR[l+1] = temp;
                    }
                }
            }
			
			for(k=0;k<81;k++){
				for(l=0;l<81;l++){
					if(pixelG[l] < pixelG[l+1]) {
                        temp = pixelG[l];
                        pixelG[l] = pixelG[l+1];
                        pixelG[l+1] = temp;
                    }
                }
            }
			
			for(k=0;k<81;k++){
				for(l=0;l<81;l++){
					if(pixelB[l] < pixelB[l+1]) {
                        temp = pixelB[l];
                        pixelB[l] = pixelB[l+1];
                        pixelB[l+1] = temp;
                    }
                }
            }
			
			
			
			
			//total += sobel;
			
			*(pTemp+0) = pixelR[41];
			*(pTemp+1) = pixelG[41];
			*(pTemp+2) = pixelB[41];
			pTemp += 4;
		}
		pTemp +=32 ;
		pBufTemp += 32;
	} 
	// 2. Top
	pTemp = (unsigned char *)pSrc;
	pBufTemp = (unsigned char *)pBuf;
	for(int w=0; w<width; w++) 
	{
		*(pTemp+0) = *(pBufTemp+0);
		*(pTemp+1) = *(pBufTemp+1);
		*(pTemp+2) = *(pBufTemp+2);
		pTemp += 4;
		pBufTemp += 4;
	}
	// 3. Bottom
	pBufTemp = (unsigned char *)((unsigned long)pBuf + width * (height-1) * 4 );
	pTemp = (unsigned char *)((unsigned long)pSrc + width * (height-1) * 4 );
	for(int w=0; w<width; w++) 
	{
		*(pTemp+0) = *(pBufTemp+0);
		*(pTemp+1) = *(pBufTemp+1);
		*(pTemp+2) = *(pBufTemp+2);
		pTemp += 4;
		pBufTemp += 4;
	}
	// 4. Left
	pBufTemp = (unsigned char *)pBuf;
	pTemp = (unsigned char *)pSrc;
	for(int h=0; h<height; h++)
	{
		*(pTemp+0) = *(pBufTemp+0);
		*(pTemp+1) = *(pBufTemp+1);
		*(pTemp+2) = *(pBufTemp+2);
		pTemp = pTemp + line_size;
		pBufTemp = pBufTemp + line_size;
	}
	// 5. Right
	pBufTemp = (unsigned char *)((unsigned long)pBuf + (width-1) * 4 );
	pTemp = (unsigned char *)((unsigned long)pSrc + (width-1) * 4 );
	for(int h=0; h<height; h++) 
	{
		*(pTemp+0) = *(pBufTemp+0);
		*(pTemp+1) = *(pBufTemp+1);
		*(pTemp+2) = *(pBufTemp+2);
		pTemp = pTemp + line_size;
		pBufTemp = pBufTemp + line_size;
	}
	
	return;
	
}
void iPhonePhotoMedianFilter5x5(unsigned char* pSrc,unsigned int width,unsigned int height){
	int l,k;
	unsigned int buf_size,line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char pBuf[width*4][height];
	unsigned char pixelR[25],pixelG[25],pixelB[25],temp;
	line_size=width*4;
	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	for(int i=0; i<buf_size; i++)
	{
		*(pBufTemp+i) = *(pTemp+i);
	}
	
	
	
	pTemp+=line_size*2+4*2;
	for(int h=2; h<(height-2); h++)
	{
		for(int w=2; w<(width-2); w++)
		{
			// line - 1
			pixelR[0] = *pBufTemp;
			pixelG[0] = *(pBufTemp+1);
			pixelB[0] += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[1] = *pBufTemp;
			pixelG[1] = *(pBufTemp+1);
			pixelB[1] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[2] = *pBufTemp;
			pixelG[2] = *(pBufTemp+1);
			pixelB[2] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[3] = *pBufTemp;
			pixelG[3] = *(pBufTemp+1);
			pixelB[3] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[4] = *pBufTemp;
			pixelG[4] = *(pBufTemp+1);
			pixelB[4] = *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 16;
			// line - 1
			pixelR[5] = *pBufTemp;
			pixelG[5] = *(pBufTemp+1);
			pixelB[5] += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[6] = *pBufTemp;
			pixelG[6] = *(pBufTemp+1);
			pixelB[6] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[7] = *pBufTemp;
			pixelG[7] = *(pBufTemp+1);
			pixelB[7] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[8] = *pBufTemp;
			pixelG[8] = *(pBufTemp+1);
			pixelB[8] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[9] = *pBufTemp;
			pixelG[9] = *(pBufTemp+1);
			pixelB[9] = *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 16;
			pixelR[10] = *pBufTemp;
			pixelG[10] = *(pBufTemp+1);
			pixelB[10] += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[11] = *pBufTemp;
			pixelG[11] = *(pBufTemp+1);
			pixelB[11] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[12] = *pBufTemp;
			pixelG[12] = *(pBufTemp+1);
			pixelB[12] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[13] = *pBufTemp;
			pixelG[13] = *(pBufTemp+1);
			pixelB[13] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[14] = *pBufTemp;
			pixelG[14] = *(pBufTemp+1);
			pixelB[14] = *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 16;
			pixelR[15] = *pBufTemp;
			pixelG[15] = *(pBufTemp+1);
			pixelB[15] += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[16] = *pBufTemp;
			pixelG[16] = *(pBufTemp+1);
			pixelB[16] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[17] = *pBufTemp;
			pixelG[17] = *(pBufTemp+1);
			pixelB[17] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[18] = *pBufTemp;
			pixelG[18] = *(pBufTemp+1);
			pixelB[18] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[19] = *pBufTemp;
			pixelG[19] = *(pBufTemp+1);
			pixelB[19] = *(pBufTemp+2);
			pBufTemp = pBufTemp + line_size - 16;
			pixelR[20] = *pBufTemp;
			pixelG[20] = *(pBufTemp+1);
			pixelB[20] += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[21] = *pBufTemp;
			pixelG[21] = *(pBufTemp+1);
			pixelB[21] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[22] = *pBufTemp;
			pixelG[22] = *(pBufTemp+1);
			pixelB[22] = *(pBufTemp+2);
			pBufTemp += 4;
			pixelR[23] = *pBufTemp;
			pixelG[23] = *(pBufTemp+1);
			pixelB[23] = *(pBufTemp+2);
		  	pBufTemp += 4;
		 	pixelR[24] = *pBufTemp;
			pixelG[24] = *(pBufTemp+1);
			pixelB[24] = *(pBufTemp+2);
			pBufTemp = pBufTemp - (line_size << 2) - 12;
			
			
			for(k=0;k<25;k++){
				for(l=0;l<25;l++){
					if(pixelR[l] < pixelR[l+1]) {
                        temp = pixelR[l];
                        pixelR[l] = pixelR[l+1];
                        pixelR[l+1] = temp;
                    }
                }
            }
			
			for(k=0;k<25;k++){
				for(l=0;l<25;l++){
					if(pixelG[l] < pixelG[l+1]) {
                        temp = pixelG[l];
                        pixelG[l] = pixelG[l+1];
                        pixelG[l+1] = temp;
                    }
                }
            }
			
			for(k=0;k<25;k++){
				for(l=0;l<25;l++){
					if(pixelB[l] < pixelB[l+1]) {
                        temp = pixelB[l];
                        pixelB[l] = pixelB[l+1];
                        pixelB[l+1] = temp;
                    }
                }
            }
			
			
			
			
			//total += sobel;
			
			*(pTemp+0) = pixelR[12];
			*(pTemp+1) = pixelG[12];
			*(pTemp+2) = pixelB[12];
			pTemp += 4;
		}
		pTemp +=16 ;
		pBufTemp += 16;
	} 
	// 2. Top
	pTemp = (unsigned char *)pSrc;
	pBufTemp = (unsigned char *)pBuf;
	for(int w=0; w<width; w++) 
	{
		*(pTemp+0) = *(pBufTemp+0);
		*(pTemp+1) = *(pBufTemp+1);
		*(pTemp+2) = *(pBufTemp+2);
		pTemp += 4;
		pBufTemp += 4;
	}
	// 3. Bottom
	pBufTemp = (unsigned char *)((unsigned long)pBuf + width * (height-1) * 4 );
	pTemp = (unsigned char *)((unsigned long)pSrc + width * (height-1) * 4 );
	for(int w=0; w<width; w++) 
	{
		*(pTemp+0) = *(pBufTemp+0);
		*(pTemp+1) = *(pBufTemp+1);
		*(pTemp+2) = *(pBufTemp+2);
		pTemp += 4;
		pBufTemp += 4;
	}
	// 4. Left
	pBufTemp = (unsigned char *)pBuf;
	pTemp = (unsigned char *)pSrc;
	for(int h=0; h<height; h++)
	{
		*(pTemp+0) = *(pBufTemp+0);
		*(pTemp+1) = *(pBufTemp+1);
		*(pTemp+2) = *(pBufTemp+2);
		pTemp = pTemp + line_size;
		pBufTemp = pBufTemp + line_size;
	}
	// 5. Right
	pBufTemp = (unsigned char *)((unsigned long)pBuf + (width-1) * 4 );
	pTemp = (unsigned char *)((unsigned long)pSrc + (width-1) * 4 );
	for(int h=0; h<height; h++) 
	{
		*(pTemp+0) = *(pBufTemp+0);
		*(pTemp+1) = *(pBufTemp+1);
		*(pTemp+2) = *(pBufTemp+2);
		pTemp = pTemp + line_size;
		pBufTemp = pBufTemp + line_size;
	}
	
	return;
	
}
void iPhonePhotoFilterLV06_2(unsigned char* pSrc,unsigned int width,unsigned int height){
	unsigned int line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;
	unsigned char* pBuf2;
	//[height][width*4];
	line_size=width*4;
	
	pBuf=(unsigned char*)malloc(height*line_size);
	pBuf2=(unsigned char*)malloc(height*line_size);
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			*(pBuf+w+h*line_size)= *(pTemp+w+h*line_size);
			*(pBuf2+w+h*line_size)= *(pTemp+w+h*line_size);
		}
	}
#define GAP 10
	int val;
		  int x,y;
		  int xx,yy;
		  int hh;
		  int du,dd,dl,dr;
		  int endn;
		  int c1,c2;
		  double gamma,a;
		  int rrx,ggx,bbx;
		  int rry,ggy,bby;
		  int rrr,ggg,bbb;
		  int rr,gg,bb,gray;
		  int rate;
		  int res1,res2,res;
		  int th1,th2,th3;
		  int r1,g1,b1;
	//	  Pixel col,ncol;
		  int *sobel;
		  int sadr;
		  int sobel1[9]={
			     1, 0,-1,
			     2, 0,-2,
		     1, 0,-1};
		  int sobel2[9]={
			     1, 2, 1,
			     0, 0, 0,
		    -1,-2,-1};
		  int x1,y1,x2,y2;
	int gammaint,ol;
		  x1=0;
		  y1=0;
		  x2=width-1;
		  y2=height-1; 	/*
	double  factor[7] = { 1.25, 1.5, 1.75, 2.0, 2.5, 3.0, 4.0 };   
	int x1,y1,x2,y2;
	int act,st,sm;
	act=2;
	st=256;
	sm=200;
	x1=0;
	y1=0;
	x2=width-1;
	y2=height-1;
	
	// œ»tB^ÌÝè
	sum=0;
	for(ff=0;ff<9;ff++) {
		if(ff==4) {
			smooth[ff]=10;
			sum+=10;
		}
		else {
			smooth[ff]=sm;
			sum+=sm;
		}
	}
	
	f1 = (int)((double)FACTOR * factor[act]);
	f2 = (int)((double)FACTOR * (factor[act] - 1.0)/2.0);
	
	
	// FÊ­²
	for(y=y1;y<=y2;y++) {
		for(x=x1;x<=x2;x++) {
			   //æãÌæfîñðæŸ
			rr=*(pTemp+(x)*4+(y)*line_size);
			gg=*(pTemp+(x)*4+(y)*line_size+1);
			bb=*(pTemp+(x)*4+(y)*line_size+2); 
			ro= ( rr*f1-gg*f2-bb*f2)/FACTOR;
			go= (-rr*f2+gg*f1-bb*f2)/FACTOR;
			bo= (-rr*f2-gg*f2+bb*f1)/FACTOR;
	
			*(pBuf+(x)*4+(y)*line_size)= ro;
			*(pBuf+(x)*4+(y)*line_size+1)= go;
			*(pBuf+(x)*4+(y)*line_size+2)= bo; // obt@ÉlðZbg·é
		}
	}
	
	st2=st/2;
	con=66;
	// æs»
	//
	for(y=y1;y<=y2;y++) {
		for(x=x1;x<=x2;x++) {
			err=egg=ebb=0;
			sr_x=sg_x=sb_x=0;
			sr_y=sg_y=sb_y=0;
			ff=0;
			for(yy=-1;yy<=1;yy++) {
				for(xx=-1;xx<=1;xx++) {
					// GbWðßéÌÍC³æªÎÛ
				     //æãÌæfîñðæŸ
					// æs»tB^	
					col[0]=*(pTemp+(x+xx)*4+(y+yy)*line_size);
					col[1]=*(pTemp+(x+xx)*4+(y+yy)*line_size+1);
					col[2]=*(pTemp+(x+xx)*4+(y+yy)*line_size+2); 
					err-=col[0]*fil[ff];
					egg-=col[1]*fil[ff];
					ebb-=col[2]*fil[ff];
					
					// SobelÉæéGbWo
					sr_x+= col[0]*sobel1[ff];
					sg_x+= col[1]*sobel1[ff];
					sb_x+= col[2]*sobel1[ff];
					sr_y+= col[0]*sobel2[ff];
					sg_y+= col[1]*sobel2[ff];
					sb_y+= col[2]*sobel2[ff];
					
					ff++;
				}
			}
			// FÊ­²µœãÌlðæŸ
		 //æãÌæfîñðæŸ
			r1=*(pBuf+(x)*4+(y)*line_size);
			g1=*(pBuf+(x)*4+(y)*line_size+1);
			b1=*(pBuf+(x)*4+(y)*line_size+2);
			// GbWªðÁÉ­²
			err=err*st/100+(int)sqrt(sr_x*sr_x+sr_y*sr_y)*st2/100;
			egg=egg*st/100+(int)sqrt(sg_x*sg_x+sg_y*sg_y)*st2/100;
			ebb=ebb*st/100+(int)sqrt(sb_x*sb_x+sb_y*sb_y)*st2/100;
			rr=getVal(err,(int)col[0],con);
			gg=getVal(egg,(int)col[1],con);
			bb=getVal(ebb,(int)col[2],con);
			col[0] = rr;
			col[1] = gg;
			col[2] = bb;
			*(pBuf+(x)*4+(y)*line_size)= col[0];
			*(pBuf+(x)*4+(y)*line_size+1)= col[1];
			*(pBuf+(x)*4+(y)*line_size+2)= col[2]; // obt@ÉlðZbg·é
		}
	}
	// œ»
	for(y=y1;y<=y2;y++) {
		for(x=x1;x<=x2;x++) {
			err=egg=ebb=0;
			ff=0;
			for(yy=-1;yy<=1;yy++) {
				for(xx=-1;xx<=1;xx++) {
					col[0]=*(pBuf+(x+xx)*4+(y+yy)*line_size);
					col[1]=*(pBuf+(x+xx)*4+(y+yy)*line_size+1);
					col[2]=*(pBuf+(x+xx)*4+(y+yy)*line_size+2);    //æãÌæfîñðæŸ
					err+=col[0]*smooth[ff];
					egg+=col[1]*smooth[ff];
					ebb+=col[2]*smooth[ff];
					
					ff++;
				}
			}
			col[0] = err/sum;
			col[1] = egg/sum;
			col[2] = ebb/sum;
			*(pBuf2+(x)*4+(y)*line_size)= col[0];
			*(pBuf2+(x)*4+(y)*line_size+1)= col[1];
			*(pBuf2+(x)*4+(y)*line_size+2)= col[2];     // æÉlðZbg·é
		}
	} */
	
	x1=0;
	y1=0;
	x2=width-1;
	y2=height-1;
	gammaint=180;
	ol=4;
	
	gamma=(double)gammaint/100.0;
	gamma=1.0/gamma;
	a=128.0/255.0;
	th1=(int)(pow(a,gamma)*255.0);
	a=96.0/255.0;
	th2=(int)(pow(a,gamma)*255.0);
	a=64.0/255.0;
	th3=(int)(pow(a,gamma)*255.0);
	for(y=y1;y<=y2;y++) {
		for(x=x1;x<=x2;x++) {
			rrx=ggx=bbx=0;
			rry=ggy=bby=0;
			sadr=0;
			for(yy=0;yy<3;yy++) {
				for(xx=0;xx<3;xx++) {
					//æãÌæfîñðæŸ
					
					rr=*(pTemp+(x+xx-1)*4+(y+yy-1)*line_size);
					gg=*(pTemp+(x+xx-1)*4+(y+yy-1)*line_size+1);
					bb=*(pTemp+(x+xx-1)*4+(y+yy-1)*line_size+2); 

					rrx+= rr*sobel1[sadr];
					rry+= rr*sobel2[sadr];
					ggx+= gg*sobel1[sadr];
					ggy+= gg*sobel2[sadr];
					bbx+= bb*sobel1[sadr];
					bby+= bb*sobel2[sadr];
					sadr++;
				}
			}
			rrr=(int)(sqrt((double)(rrx*rrx+rry*rry))/8.0);
			ggg=(int)(sqrt((double)(ggx*ggx+ggy*ggy))/8.0);
			bbb=(int)(sqrt((double)(bbx*bbx+bby*bby))/8.0);
			
			////////
			if(rrr>ggg) rate=rrr;
			else rate=ggg;
			if(bbb>rate) rate=bbb;
			
			if(rate<ol) res1=255;
			else if(rate<ol+GAP) {
				res1=255-255*(rate-ol)/GAP;
			}
			else res1=0;
			
			if(rrr<ol) rrr=255;
			else if(rrr<ol+GAP) {
				rrr=255-255*(rrr-ol)/GAP;
			}
			else rrr=0;
			
			if(ggg<ol) ggg=255;
			else if(ggg<ol+GAP) {
				ggg=255-255*(ggg-ol)/GAP;
			}
			else res1=0;
			
			if(bbb<ol) bbb=255;
			else if(bbb<ol+GAP) {
				bbb=255-255*(bbb-ol)/GAP;
			}
			else bbb=0;
			
			 //æãÌæfîñðæŸ
			rr=*(pTemp+(x)*4+(y)*line_size);
			gg=*(pTemp+(x)*4+(y)*line_size+1);
			bb=*(pTemp+(x)*4+(y)*line_size+2); 
			gray=(bb*28+77*rr+gg*151)/256;
			
			if(gray>th1)     res2=255;
			else if(gray>th2) res2=128;
			else if(gray>th3) res2=64;
			else res2=0;
			if(rr>th1)     rr=255;
			else if(rr>th2) rr=128;
			else if(rr>th3) rr=64;
			else rr=0;
			
			if(gg>th1)     gg=255;
			else if(gg>th2) gg=128;
			else if(gg>th3) gg=64;
			else gg=0;
			
			if(bb>th1)     bb=255;
			else if(bb>th2) bb=128;
			else if(bb>th3) bb=64;
			else bb=0;
			
			rr=res1*res2/256;
			gg=res1*res2/256;
			bb=res1*res2/256;
			////////////
	 					
			*(pBuf2+(x)*4+(y)*line_size)= rr;
			*(pBuf2+(x)*4+(y)*line_size+1)= gg;
			*(pBuf2+(x)*4+(y)*line_size+2)= bb;  // æÉlðZbg·é
		}
	}
	 /*모자이크
	 
	 int val;
	 int x,y;
	 int xx,yy;
	 int sx,sy;
	 int ex,ey;
	 int bx,by;
	 int rr,gg,bb;
	 int dum,dum2;
	 int endn;
	 int mm;
	 int mlx,mly,zzx;
	 int my1,my2;
	 int col[3];
	 int x1,y1,x2,y2;
	 int msx,msy;
	 x1=0;
	 y1=0;
	 x2=width-1;
	 y2=height-1;
	 msx=10;
	 msy=10;
	 if(msx==0) msx=1;
	 if(msy==0) msy=1;
	 mlx=(x2-x1+1+msx-1)/msx;
	 mly=(y2-y1+1+msy-1)/msy;
	 zzx=msx/2;
	 for(y=0;y<mly;y++) {
	 bx=0;
	 for(x=bx;x<x2;x+=msx) {
	 rr=gg=bb=0;
	 sx=x;
	 if(x<0) sx=0;
	 sy=y1+y*msy;
	 ex=x+msx-1;
	 ey=sy+msy-1;
	 if(ex>x2) ex=x2;
	 if(ey>y2) ey=y2;
	 for(yy=sy;yy<=ey;yy++) {
	 for(xx=sx;xx<=ex;xx++) {
	 val =*(pBuf+xx*4+yy*line_size);//getPixel(img,xx,yy,&col); //æãÌæfîñðæŸ
	 rr+=*(pBuf+xx*4+yy*line_size);
	 gg+=*(pBuf+xx*4+yy*line_size+1);
	 bb+=*(pBuf+xx*4+yy*line_size+2);
	 }
	 }
	 mm=(ex-sx+1)*(ey-sy+1);
	 if(mm) {
	 rr/=mm;
	 gg/=mm;
	 bb/=mm;
	 col[0] = rr;
	 col[1] = gg;
	 col[2] = bb;
	 for(yy=sy;yy<=ey;yy++) {
	 for(xx=sx;xx<=ex;xx++) {
	 *(pBuf2+xx*4+yy*line_size)=col[0];
	 *(pBuf2+xx*4+yy*line_size+1)=col[1];
	 *(pBuf2+xx*4+yy*line_size+2)=col[2];    // æÉlðZbg·é
	 }
	 }
	 }
	 }
	 } */
	
	/*
	 iPhonePhotoOverlayFilter((unsigned char*)pBuf,width,height);
	 iPhonePhotoOverlayFilter((unsigned char*)pBuf2,width,height);
	 iPhonePhotoOverlayFilter((unsigned char*)pBuf2,width,height);
	 
	 //iPhonePhotoInvertFilter((unsigned char*)pBuf,width,height);
	 
	 
	 
	 pTemp=pSrc;
	 for(int h=0; h<(height); h++)
	 {
	 for(int w=0; w<(width*4); w+=4)
	 
	 
	 {
	 
	 if(rand()%(h%5*h%5+1)==0){
	 if((w+h*10)%100>90&&rand()%3){						
	 if(w<12){
	 *(pTemp+w+h*line_size) =*(pBuf+w+h*line_size);
	 *(pTemp+w+h*line_size+1) =*(pBuf+w+h*line_size+1);
	 *(pTemp+w+h*line_size+2) =*(pBuf+w+h*line_size+2);
	 }
	 else if(w<20){
	 *(pTemp+w+h*line_size) =*(pBuf+w+h*line_size-8);
	 *(pTemp+w+h*line_size+1) =*(pBuf+w+h*line_size+1-8);
	 *(pTemp+w+h*line_size+2) =*(pBuf+w+h*line_size+2-8);
	 }
	 else if(w<28){
	 *(pTemp+w+h*line_size) =*(pBuf+w+h*line_size-16);
	 *(pTemp+w+h*line_size+1) =*(pBuf+w+h*line_size+1-16);
	 *(pTemp+w+h*line_size+2) =*(pBuf+w+h*line_size+2-16);
	 }
	 else if(w<36){
	 *(pTemp+w+h*line_size) =*(pBuf+w+h*line_size-24);
	 *(pTemp+w+h*line_size+1) =*(pBuf+w+h*line_size+1-24);
	 *(pTemp+w+h*line_size+2) =*(pBuf+w+h*line_size+2-24);
	 }
	 else{
	 *(pTemp+w+h*line_size) =*(pBuf+w+h*line_size-32);
	 *(pTemp+w+h*line_size+1) =*(pBuf+w+h*line_size-32+1);
	 *(pTemp+w+h*line_size+2) =*(pBuf+w+h*line_size-32+2);
	 }
	 }else{
	 if(w<12){
	 *(pTemp+w+h*line_size) =*(pBuf2+w+h*line_size);
	 *(pTemp+w+h*line_size+1) =*(pBuf2+w+h*line_size+1);
	 *(pTemp+w+h*line_size+2) =*(pBuf2+w+h*line_size+2);
	 }
	 else if(w<20){
	 *(pTemp+w+h*line_size) =*(pBuf2+w+h*line_size-8);
	 *(pTemp+w+h*line_size+1) =*(pBuf2+w+h*line_size+1-8);
	 *(pTemp+w+h*line_size+2) =*(pBuf2+w+h*line_size+2-8);
	 }
	 else if(w<28){
	 *(pTemp+w+h*line_size) =*(pBuf2+w+h*line_size-16);
	 *(pTemp+w+h*line_size+1) =*(pBuf2+w+h*line_size+1-16);
	 *(pTemp+w+h*line_size+2) =*(pBuf2+w+h*line_size+2-16);
	 }
	 else if(w<36){
	 *(pTemp+w+h*line_size) =*(pBuf2+w+h*line_size-24);
	 *(pTemp+w+h*line_size+1) =*(pBuf2+w+h*line_size+1-24);
	 *(pTemp+w+h*line_size+2) =*(pBuf2+w+h*line_size+2-24);
	 }
	 else{
	 *(pTemp+w+h*line_size) =*(pBuf2+w+h*line_size-32);
	 *(pTemp+w+h*line_size+1) =*(pBuf2+w+h*line_size-32+1);
	 *(pTemp+w+h*line_size+2) =*(pBuf2+w+h*line_size-32+2);
	 }
	 
	 
	 
	 
	 }
	 
	 }		
	 else{
	 if((w+h)%100<90&&rand()%3){						
	 *(pTemp+w+h*line_size) =*(pBuf+w+h*line_size);
	 *(pTemp+w+h*line_size+1) =*(pBuf+w+h*line_size+1);
	 *(pTemp+w+h*line_size+2) =*(pBuf+w+h*line_size+2);
	 }
	 
	 
	 }	
	 
	 
	 
	 
	 }
	 }*/
	
	
	pTemp=pSrc;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			*(pTemp+w+h*line_size) =(*(pBuf2+w+h*line_size))*(*(pTemp+w+h*line_size))/255;
		}
	}
	if(pBuf!=NULL)free(pBuf);
	if(pBuf2!=NULL)free(pBuf2);
}
void iPhonePhotoFilterLV06_1(unsigned char* pSrc,unsigned int width,unsigned int height){
	unsigned int line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char pBuf[height][width*4];
	unsigned char pBuf2[height][width*4];
	unsigned char pBuf3[height][width*4];
	unsigned char pBuf4[height][width*4];
	line_size=width*4;
	
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			pBuf[h][w] = *(pTemp+w+h*line_size);
		}
	}
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			pBuf2[h][w] = *(pTemp+w+h*line_size);
		}
	}
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			pBuf3[h][w] = *(pTemp+w+h*line_size);
		}
	}
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			pBuf4[h][w] = *(pTemp+w+h*line_size);
		}
	}
	iPhonePhotoGrayFilter1((unsigned char*)pBuf,width,height,50);
	//iPhonePhoto16bitColor((unsigned char*)pBuf,width,height);
	iPhonePhotoMeanFilter((unsigned char*)pBuf,width,height);
	iPhonePhotoPrewittFilter((unsigned char*)pBuf,width,height);
	
	
	iPhonePhotoInvertFilter((unsigned char*)pBuf,width,height);
	//iPhonePhotoFilterTest((unsigned char*)pBuf,width,height);
	iPhonePhotoBinaryFilterGray1((unsigned char*)pBuf,width,height);
	//
	//iPhonePhotoMedianFilter9x9((unsigned char*)pBuf2,width,height);
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//if(pBuf2[h][w]>pBuf[h][w])pBuf3[h][w]=pBuf2[h][w];
			//else pBuf3[h][w]=pBuf[h][w];
			pBuf3[h][w]=pBuf[h][w];//*pBuf2[h][w]/255;
		}
	}
	pTemp=pSrc;
	//iPhonePhotoMedianFilter5x5((unsigned char*)pBuf3,width,height);
	/*
	 for(int h=0; h<(height); h++)
	 {
	 for(int w=0; w<(width*4); w++)
	 {
	 //	pBuf3[h][w]=pBuf3[h][w]*pBuf3[h][w]/255;
	 if(pBuf3[h][w]>0)pBuf3[h][w]=pBuf3[h][w]*pBuf4[h][w]/255;
	 
	 
	 }
	 }
	 */
	//iPhonePhoto16bitColor((unsigned char*)pBuf3,width,height);
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			
			*(pTemp+w+h*line_size)=pBuf3[h][w];
			
			
		}
	}
	
	//	iPhonePhotoOverlayFilter(pSrc,width,height);
	//	iPhonePhotoOverlayFilter(pSrc,width,height);
	//iPhonePhotoOverlayFilter(pSrc,width,height);
	
}

void iPhonePhotoFilterTest(unsigned char* pSrc,unsigned int width,unsigned int height){
	unsigned int buf_size,line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;//[height+2][width*4+8];
	int PrewittR,PrewittG,PrewittB;
	int row_pixelR,row_pixelG,row_pixelB;
	line_size=width*4;
	buf_line_size=width*4+8;
	buf_size=height*line_size;
	pTemp=pSrc;
	pBuf=(unsigned char*)malloc((height+2)*buf_line_size);
	pBufTemp=(unsigned char*) pBuf;
	
	for(int w=0; w<(width*4); w++)
	{
		*(pBuf+w+4)=*(pTemp+w);
	}
	
	
	for(int w=0; w<(width*4); w++)
	{
		//		pBuf[height+1][w+4] 
		*(pBuf+w+4+(height+1)*buf_line_size)=*(pTemp+w+(height-1)*line_size);
	}
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<4; w++)
		{
			//pBuf[h+1][w] 
			*(pBuf+(h+1)*buf_line_size+w)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<4; w++)
		{
			//pBuf[h+1][w+4+width*4] 
			*(pBuf+(h+1)*buf_line_size+w+4+width*4)=*(pTemp+(width-1)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//pBuf[h+1][w+4] 
			*(pBuf+(h+1)*buf_line_size+w+4)= *(pTemp+w+h*line_size);
		}
	}
	
	
	pTemp=pSrc;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width); w++)
		{
 			row_pixelR = 255;
			row_pixelG = 255;
			row_pixelB = 255;
			// line - 1
	
		  	pBufTemp += 4;
			if(*pBufTemp<128)row_pixelR =0;
			if(*(pBufTemp+1)<128)row_pixelG = 0;
			if(*(pBufTemp+2)<128)row_pixelB = 0;
			pBufTemp += 4;
	
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 2
			if(*pBufTemp<128)row_pixelR =0;
			if(*(pBufTemp+1)<128)row_pixelG = 0;
			if(*(pBufTemp+2)<128)row_pixelB = 0;
			pBufTemp += 4;
			pBufTemp += 4;
			if(*pBufTemp<128)row_pixelR =0;
			if(*(pBufTemp+1)<128)row_pixelG = 0;
			if(*(pBufTemp+2)<128)row_pixelB = 0;
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 3

		  	pBufTemp += 4;
			if(*pBufTemp<128)row_pixelR =0;
			if(*(pBufTemp+1)<128)row_pixelG = 0;
			if(*(pBufTemp+2)<128)row_pixelB = 0;
			pBufTemp += 4;
	
			pBufTemp = pBufTemp - (buf_line_size << 1) - 4;

			
			

			PrewittR = row_pixelR;
			PrewittG = row_pixelG;
			PrewittB = row_pixelB;
			//total += sobel;
			
			*(pTemp+0) = PrewittR;
			*(pTemp+1) = PrewittG;
			*(pTemp+2) = PrewittB;
			pTemp += 4;
		}
		pBufTemp += 8;
	} 
	if(pBuf!=NULL)free(pBuf);
	return;
	

}
void iPhonePhotoPrewittFilter1(unsigned char* pSrc,unsigned int width,unsigned int height){	
	unsigned int buf_size,line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;//[height+2][width*4+8];
	int PrewittR,PrewittG,PrewittB;
	int cols_pixelR,cols_pixelG,cols_pixelB;
	int row_pixelR,row_pixelG,row_pixelB;
	line_size=width*4;
	buf_line_size=width*4+8;
	buf_size=height*line_size;
	pTemp=pSrc;
	pBuf=(unsigned char*)malloc((height+2)*buf_line_size);
	pBufTemp=(unsigned char*) pBuf;
	
	for(int w=0; w<(width*4); w++)
	{
		*(pBuf+w+4)=*(pTemp+w);
	}
	
	
	for(int w=0; w<(width*4); w++)
	{
		//		pBuf[height+1][w+4] 
		*(pBuf+w+4+(height+1)*buf_line_size)=*(pTemp+w+(height-1)*line_size);
	}
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<4; w++)
		{
			//pBuf[h+1][w] 
			*(pBuf+(h+1)*buf_line_size+w)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<4; w++)
		{
			//pBuf[h+1][w+4+width*4] 
			*(pBuf+(h+1)*buf_line_size+w+4+width*4)=*(pTemp+(width-1)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//pBuf[h+1][w+4] 
			*(pBuf+(h+1)*buf_line_size+w+4)= *(pTemp+w+h*line_size);
		}
	}
	
	
	pTemp=pSrc;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width); w++)
		{
			row_pixelR = 0;
			row_pixelG = 0;
			row_pixelB = 0;
			// line - 1
			row_pixelR += *pBufTemp;
			row_pixelG += *(pBufTemp+1);
			row_pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			row_pixelR += *pBufTemp*2;
			row_pixelG += *(pBufTemp+1)*2;
			row_pixelB += *(pBufTemp+2)*2;
			pBufTemp += 4;
			row_pixelR += *pBufTemp;
			row_pixelG += *(pBufTemp+1);
			row_pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 2
			pBufTemp += 4;
			pBufTemp += 4;
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 3
			row_pixelR -= *pBufTemp;
			row_pixelG -= *(pBufTemp+1);
			row_pixelB -= *(pBufTemp+2);
			pBufTemp += 4;
			row_pixelR -= *pBufTemp*2;
			row_pixelG -= *(pBufTemp+1)*2;
			row_pixelB -= *(pBufTemp+2)*2;
			pBufTemp += 4;
			row_pixelR -= *pBufTemp;
			row_pixelG -= *(pBufTemp+1);
			row_pixelB -= *(pBufTemp+2);
			pBufTemp = pBufTemp - (buf_line_size << 1) - 8;
			
			cols_pixelR = 0;
			cols_pixelG = 0;
			cols_pixelB = 0;
			// line - 1
			cols_pixelR += *pBufTemp;
			cols_pixelG += *(pBufTemp+1);
			cols_pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pBufTemp += 4;
			cols_pixelR -= *pBufTemp;
			cols_pixelG -= *(pBufTemp+1);
			cols_pixelB -= *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 2
			cols_pixelR += *pBufTemp*2;
			cols_pixelG += *(pBufTemp+1)*2;
			cols_pixelB += *(pBufTemp+2)*2;
			pBufTemp += 4;
			pBufTemp += 4;
			cols_pixelR -= *pBufTemp*2;
			cols_pixelG -= *(pBufTemp+1)*2;
			cols_pixelB -= *(pBufTemp+2)*2;
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 3
			cols_pixelR += *pBufTemp;
			cols_pixelG += *(pBufTemp+1);
			cols_pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			pBufTemp += 4;
			cols_pixelR -= *pBufTemp;
			cols_pixelG -= *(pBufTemp+1);
			cols_pixelB -= *(pBufTemp+2);
			pBufTemp = pBufTemp - (buf_line_size << 1) - 4;
			
			if(row_pixelR<0) row_pixelR *= (-1);
			if(row_pixelG<0) row_pixelG *= (-1);
			if(row_pixelB<0) row_pixelB *= (-1);
			if(cols_pixelR<0) cols_pixelR *= (-1);
			if(cols_pixelG<0) cols_pixelG *= (-1);
			if(cols_pixelB<0) cols_pixelB *= (-1);
			PrewittR = row_pixelR + cols_pixelR;
			PrewittG = row_pixelG + cols_pixelG;
			PrewittB = row_pixelB + cols_pixelB;
			if(PrewittR>255)PrewittR=255;
			if(PrewittG>255)PrewittG=255;
			if(PrewittB>255)PrewittB=255;
			//total += sobel;
			
			*(pTemp+0) = PrewittR;
			*(pTemp+1) = PrewittG;
			*(pTemp+2) = PrewittB;
			pTemp += 4;
		}
		pBufTemp += 8;
	} 
	if(pBuf!=NULL)free(pBuf);
	return;
	
}

void iPhonePhotoPrewittFilter2(unsigned char* pSrc,unsigned int width,unsigned int height){
	unsigned int buf_size,line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;//[height+2][width*4+8];
	int PrewittR,PrewittG,PrewittB;
	int cols_pixelR,cols_pixelG,cols_pixelB;
	int row_pixelR,row_pixelG,row_pixelB;
	line_size=width*4;
	buf_line_size=width*4+8;
	buf_size=height*line_size;
	pTemp=pSrc;
	pBuf=(unsigned char*)malloc((height+2)*buf_line_size);
	pBufTemp=(unsigned char*) pBuf;
	
	for(int w=0; w<(width*4); w++)
	{
		*(pBuf+w+4)=*(pTemp+w);
	}
	
	
	for(int w=0; w<(width*4); w++)
	{
		//		pBuf[height+1][w+4] 
		*(pBuf+w+4+(height+1)*buf_line_size)=*(pTemp+w+(height-1)*line_size);
	}
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<4; w++)
		{
			//pBuf[h+1][w] 
			*(pBuf+(h+1)*buf_line_size+w)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<4; w++)
		{
			//pBuf[h+1][w+4+width*4] 
			*(pBuf+(h+1)*buf_line_size+w+4+width*4)=*(pTemp+(width-1)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//pBuf[h+1][w+4] 
			*(pBuf+(h+1)*buf_line_size+w+4)= *(pTemp+w+h*line_size);
		}
	}
	
	
	pTemp=pSrc;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width); w++)
		{
 			row_pixelR = 0;
			row_pixelG = 0;
			row_pixelB = 0;
			// line - 1
			row_pixelR += *pBufTemp;
			row_pixelG += *(pBufTemp+1);
			row_pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	row_pixelR += *pBufTemp;
			row_pixelG += *(pBufTemp+1);
			row_pixelB += *(pBufTemp+2);
			pBufTemp += 4;
			row_pixelR += *pBufTemp;
			row_pixelG += *(pBufTemp+1);
			row_pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 2
			pBufTemp += 4;
			pBufTemp += 4;
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 3
			row_pixelR -= *pBufTemp;
			row_pixelG -= *(pBufTemp+1);
			row_pixelB -= *(pBufTemp+2);
		  	pBufTemp += 4;
		 	row_pixelR -= *pBufTemp;
			row_pixelG -= *(pBufTemp+1);
			row_pixelB -= *(pBufTemp+2);
			pBufTemp += 4;
			row_pixelR -= *pBufTemp;
			row_pixelG -= *(pBufTemp+1);
			row_pixelB -= *(pBufTemp+2);
			pBufTemp = pBufTemp - (buf_line_size << 1) - 8;
			
			cols_pixelR = 0;
			cols_pixelG = 0;
			cols_pixelB = 0;
			// line - 1
			cols_pixelR += *pBufTemp;
			cols_pixelG += *(pBufTemp+1);
			cols_pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
			pBufTemp += 4;
			cols_pixelR -= *pBufTemp;
			cols_pixelG -= *(pBufTemp+1);
			cols_pixelB -= *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 2
			cols_pixelR += *pBufTemp;
			cols_pixelG += *(pBufTemp+1);
			cols_pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
			pBufTemp += 4;
			cols_pixelR -= *pBufTemp;
			cols_pixelG -= *(pBufTemp+1);
			cols_pixelB -= *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 3
			cols_pixelR += *pBufTemp;
			cols_pixelG += *(pBufTemp+1);
			cols_pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
			pBufTemp += 4;
			cols_pixelR -= *pBufTemp;
			cols_pixelG -= *(pBufTemp+1);
			cols_pixelB -= *(pBufTemp+2);
			pBufTemp = pBufTemp - (buf_line_size << 1) - 4;
			
			if(row_pixelR<0) row_pixelR *= (-1);
			if(row_pixelG<0) row_pixelG *= (-1);
			if(row_pixelB<0) row_pixelB *= (-1);
			if(cols_pixelR<0) cols_pixelR *= (-1);
			if(cols_pixelG<0) cols_pixelG *= (-1);
			if(cols_pixelB<0) cols_pixelB *= (-1);
			PrewittR = row_pixelR;
			PrewittG = row_pixelG;
			PrewittB = row_pixelB;
			//total += sobel;
			
			*(pTemp+0) = PrewittR;
			*(pTemp+1) = PrewittG;
			*(pTemp+2) = PrewittB;
			pTemp += 4;
		}
		pBufTemp += 8;
	} 
	if(pBuf!=NULL)free(pBuf);
	return;
	
}
void iPhonePhotoMedianFilter5x5G(unsigned char* pSrc,unsigned int width,unsigned int height){
	
	unsigned int line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;
	unsigned char* pixelBuf;
	unsigned char pixel[4],temp;
	line_size=width*4;
	buf_line_size=width*4+16;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	pBuf=(unsigned char*)malloc((height+4)*buf_line_size);
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//			pBuf[h][w+8]
			*(pBuf+h*buf_line_size+w+8)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//			pBuf[h+height+2][w+8]
			*(pBuf+(h+height+2)*buf_line_size+w+8)= *(pTemp+w+(height-2+h)*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			//pBuf[h+2][w] 
			*(pBuf+(h+2)*buf_line_size+w)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			//	pBuf[h+2][w+8+width*4]
			*(pBuf+(h+2)*buf_line_size+w+8+width*4)= *(pTemp+(width-2)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//pBuf[h+2][w+8] 
			*(pBuf+(h+2)*buf_line_size+w+8)= *(pTemp+w+h*line_size);
		}
	}
	
	
	
	for(int h=0; h<height; h++)
	{
		for(int w=0; w<width; w++)
		{
			pixelBuf=pixel;
			for(int i=0;i<4;i++)*(pixelBuf++)=0;
			
			// line - 1
			(*(pixel+(*(pBufTemp))/64))++;
	
		  	
		 	(*(pixel+(*(pBufTemp+4))/64))++;
			
			(*(pixel+(*(pBufTemp+8))/64))++;
			
			(*(pixel+(*(pBufTemp+12))/64))++;
		  	
		 	(*(pixel+(*(pBufTemp+16))/64))++;
			
						// line - 1
			(*(pixel+(*(pBufTemp+ buf_line_size))/64))++;
			
		  	
		 	(*(pixel+(*(pBufTemp+4+ buf_line_size))/64))++;
			
			(*(pixel+(*(pBufTemp+8+ buf_line_size))/64))++;
			
			(*(pixel+(*(pBufTemp+12+ buf_line_size))/64))++;
		  	
		 	(*(pixel+(*(pBufTemp+16+ buf_line_size))/64))++;

		
			(*(pixel+(*(pBufTemp+ buf_line_size*2))/64))++;
			
		  	
		 	(*(pixel+(*(pBufTemp+4+ buf_line_size*2))/64))++;
			
			(*(pixel+(*(pBufTemp+8+ buf_line_size*2))/64))++;
			
			(*(pixel+(*(pBufTemp+12+ buf_line_size*2))/64))++;
		  	
		 	(*(pixel+(*(pBufTemp+16+ buf_line_size*2))/64))++;
			
		
			(*(pixel+(*(pBufTemp+ buf_line_size*3))/64))++;
			
		  	
		 	(*(pixel+(*(pBufTemp+4+ buf_line_size*3))/64))++;
			
			(*(pixel+(*(pBufTemp+8+ buf_line_size*3))/64))++;
			
			(*(pixel+(*(pBufTemp+12+ buf_line_size*3))/64))++;
		  	
		 	(*(pixel+(*(pBufTemp+16+ buf_line_size*3))/64))++;
			
	
			(*(pixel+(*(pBufTemp+ buf_line_size*4))/64))++;
			
		  	
		 	(*(pixel+(*(pBufTemp+4+ buf_line_size*4))/64))++;
			
			(*(pixel+(*(pBufTemp+8+ buf_line_size*4))/64))++;
			
			(*(pixel+(*(pBufTemp+12+ buf_line_size*4))/64))++;
		  	
		 	(*(pixel+(*(pBufTemp+16+ buf_line_size*4))/64))++;
			
			pBufTemp += 4;
			
			/*
			
			for(k=0;k<25;k++){
				for(l=0;l<25;l++){
					if(pixel[l] < pixel[l+1]) {
                        temp = pixel[l];
                        pixel[l] = pixel[l+1];
                        pixel[l+1] = temp;
                    }
                }
            }
			
			
			*/
			temp=0;
			pixelBuf=pixel;
			for(int i=0;i<4;i++,pixelBuf++){
				if(*(pixelBuf)){
				temp+=*(pixelBuf);
					if(temp>12){
						temp=i;
						break;
					}
				}
				
			}
			
			
			//total += sobel;
			
			*(pTemp+0) = temp;
			*(pTemp+1) = temp;
			*(pTemp+2) = temp;
			pTemp += 4;
		}
	
		pBufTemp += 16;
	} 
	if(pBuf!=NULL)free(pBuf);
	return;
	
}void iPhonePhotoMedianFilterG(unsigned char* pSrc,unsigned int width,unsigned int height){
	
	unsigned int line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;
	unsigned char* pixelBuf;
	unsigned char pixel[4],temp;
	line_size=width*4;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	buf_line_size=width*4+8;
	//buf_size=height*line_size;
	pTemp=pSrc;
	pBuf=(unsigned char*)malloc((height+2)*buf_line_size);
	pBufTemp=(unsigned char*) pBuf;
	
	for(int w=0; w<(width*4); w++)
	{
		*(pBuf+w+4)=*(pTemp+w);
	}
	
	
	for(int w=0; w<(width*4); w++)
	{
		//		pBuf[height+1][w+4] 
		*(pBuf+w+4+(height+1)*buf_line_size)=*(pTemp+w+(height-1)*line_size);
	}
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<4; w++)
		{
			//pBuf[h+1][w] 
			*(pBuf+(h+1)*buf_line_size+w)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<4; w++)
		{
			//pBuf[h+1][w+4+width*4] 
			*(pBuf+(h+1)*buf_line_size+w+4+width*4)=*(pTemp+(width-1)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//pBuf[h+1][w+4] 
			*(pBuf+(h+1)*buf_line_size+w+4)= *(pTemp+w+h*line_size);
		}
	}
	
	
	
	for(int h=0; h<height; h++)
	{
		for(int w=0; w<width; w++)
		{
			pixelBuf=pixel;
			for(int i=0;i<4;i++)*(pixelBuf++)=0;
			
			// line - 1
			(*(pixel+(*(pBufTemp))/64))++;
			
		  	
		 	(*(pixel+(*(pBufTemp+4))/64))++;
			
			(*(pixel+(*(pBufTemp+8))/64))++;
			
	
			
			// line - 1
			(*(pixel+(*(pBufTemp+ buf_line_size))/64))++;
			
		  	
		 	(*(pixel+(*(pBufTemp+4+ buf_line_size))/64))++;
			
			(*(pixel+(*(pBufTemp+8+ buf_line_size))/64))++;
			
	
			
			
			(*(pixel+(*(pBufTemp+ buf_line_size*2))/64))++;
			
		  	
		 	(*(pixel+(*(pBufTemp+4+ buf_line_size*2))/64))++;
			
			(*(pixel+(*(pBufTemp+8+ buf_line_size*2))/64))++;
			

			
			
			(*(pixel+(*(pBufTemp+ buf_line_size*3))/64))++;
			
		  	
		 	(*(pixel+(*(pBufTemp+4+ buf_line_size*3))/64))++;
			
			(*(pixel+(*(pBufTemp+8+ buf_line_size*3))/64))++;
			

			
			
			(*(pixel+(*(pBufTemp+ buf_line_size*4))/64))++;
			
		  	
		 	(*(pixel+(*(pBufTemp+4+ buf_line_size*4))/64))++;
			
			(*(pixel+(*(pBufTemp+8+ buf_line_size*4))/64))++;
			
	
			
			pBufTemp += 4;
			
			/*
			 
			 for(k=0;k<25;k++){
			 for(l=0;l<25;l++){
			 if(pixel[l] < pixel[l+1]) {
			 temp = pixel[l];
			 pixel[l] = pixel[l+1];
			 pixel[l+1] = temp;
			 }
			 }
			 }
			 
			 
			 */
			temp=0;
			pixelBuf=pixel;
			for(int i=0;i<4;i++,pixelBuf++){
				if(*(pixelBuf)){
					temp+=*(pixelBuf);
					if(temp>5){
						temp=i;
						break;
					}
				}
				
			}
			
			
			//total += sobel;
			
			*(pTemp+0) = temp;
			*(pTemp+1) = temp;
			*(pTemp+2) = temp;
			pTemp += 4;
		}
		
		pBufTemp += 8;
	} 
	
	return;
	
}
unsigned short* iPhonePhotoGroup(unsigned char* pSrc,unsigned int width,unsigned int height){
	unsigned int line_size,temp,cnt_t;
	unsigned char* pTemp;
	unsigned short* pBufTemp;
	unsigned short* chkTemp;
	unsigned short* chkTemp1;
	unsigned short* pBuf;

	temp=1;
	line_size=width*4;
	//	buf_size=height*line_size;
	unsigned int a = sizeof(unsigned short);
	unsigned int b = width;
	unsigned int c = height;
	unsigned int d = a * b * c;
	
	pBuf=(unsigned short*)malloc( d );
	pTemp=pSrc;
	pBufTemp=(unsigned short*) pBuf;

	cnt_t=0;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width); w++)
		{
			//pBuf[h+2][w+8] 
			*(pBuf+(h)*width+w)= 0;
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width); w++)
		{
			if(h>0&&(*(pTemp)==*(pTemp-line_size))){
				*(pBufTemp)=*(pBufTemp-width);
				
				/*
				if(w>0&&(*(pTemp)==*(pTemp-4))&&*(pBufTemp)!=*(pBufTemp-1)){
					 for(int i=0; i<=h; i++){
						 for(int j=0; j<width; j++){
							  if(*(pBuf+i*width+j)==*(pBufTemp-1))*(pBuf+i*width+j)=*(pBufTemp);
							 }
						 }
				 
				 }
				 */
 
				

				 
			}
			else if(w>0&&(*(pTemp)==*(pTemp-4)))*(pBufTemp)=*(pBufTemp-1);

			else if(h>0&&w<(width-1)&&(*(pTemp)==*(pTemp-line_size+4))){
				*(pBufTemp)=*(pBufTemp-width+1);
				
			}
		
			else *(pBufTemp)=temp++;
			pTemp+=4;
			pBufTemp++;
			
		}
	}
	
	pTemp=pSrc;
	pBufTemp=(unsigned short*) pBuf;
	chkTemp=(unsigned short*)malloc(sizeof(unsigned short)*temp);
	chkTemp1=(unsigned short*)malloc(sizeof(unsigned short)*temp);
	//if(w>0&&(*(pTemp)==*(pTemp-4))&&*(pBufTemp)!=*(pBufTemp-1))
	for(int x=0;x<temp;x++)*(chkTemp+x)=0;
	for(int x=0;x<temp;x++)*(chkTemp1+x)=0;
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width); w++){
			if(w>0&&(*(pTemp)==*(pTemp-4))&&*(pBufTemp)!=*(pBufTemp-1)){
				if(chkTemp1[*(pBufTemp-1)]==0||chkTemp1[*(pBufTemp-1)]>*(pBufTemp))chkTemp1[*(pBufTemp-1)]=*(pBufTemp);
				
				//else if(chkTemp1[*(pBufTemp-1)]==0)chkTemp1[*(pBufTemp-1)]=*(pBufTemp);
				 
				//(chkTemp1+*(pBufTemp-1))=*(pBufTemp);
					//NSLog(@"%d",)
			}
			
			
			pBufTemp++;
			pTemp+=4;
				
																			 
		}
	}
	unsigned short tt[temp];
	for(int i=1;i<temp;i++){tt[i]=chkTemp1[i];}
	//
	
	
	int index;
	int count;
	for(int i=1;i<temp;i++){
		if(chkTemp1[i]){
			index = i;
			count = 0;
			do{
				count++;
				index = chkTemp1[index];
			}while(chkTemp1[index]);
			chkTemp1[i] = index;
		}
	}

	 
	pTemp=pSrc;
	pBufTemp=(unsigned short*) pBuf;
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width); w++){
			if(chkTemp1[*(pBufTemp-1)])*(pBufTemp)=chkTemp1[*(pBufTemp)];
			
			pBufTemp++;
			
			
		}
	}
	 
	//
	
/*
	pTemp=pSrc;
	pBufTemp=(unsigned short*) pBuf;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width); w++){
			*(chkTemp+*(pBufTemp))+=1;
			pBufTemp++;
			
		}
	}
	for(int x=0;x<temp;x++)if(*(chkTemp+x)>0)*(chkTemp+x)=cnt_t++;
		
	pBufTemp=(unsigned short*) pBuf;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width); w++)
		{
			*(pBufTemp)=*(chkTemp+*(pBufTemp));
			pBufTemp++;
		}
	}
	 
 
	
	////
	
	/*
	pTemp=pSrc;
	pBufTemp=(unsigned short*) pBuf;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width); w++)
		{
			if(*(chkTemp+*(pBufTemp))>0)*(pBufTemp)=*(chkTemp+*(pBufTemp));
			pTemp+=4;
			pBufTemp++;
			
		}
	}
*/
	
	/*
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width); w++)
		{
			//pBuf[h+2][w+8] 
			*(pSrc+(h)*line_size+w*4)= *(pBuf+(h)*width+w);
			*(pSrc+(h)*line_size+w*4+1)= *(pBuf+(h)*width+w);
			*(pSrc+(h)*line_size+w*4+2)= *(pBuf+(h)*width+w);
		}
	}
	 */

	return pBuf;
	
	
}
void iPhonePhotoFilterLV6(unsigned char* pSrc,unsigned int width,unsigned int height){
	
	unsigned int line_size;
	unsigned int cnt,temp;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;
	unsigned short a ;
	unsigned short H,W;
	unsigned int* GroupR;
	unsigned int* GroupG;
	unsigned int* GroupB;
	unsigned int* pixNum;
	unsigned short* Group;
	unsigned short* GroupTemp;
	unsigned short* chkTemp;
	
	
	
	W=width;
	H=height;
	line_size=W*4;
	a=width*sizeof(unsigned short);
	pTemp=pSrc;
	pBuf=(unsigned char*)malloc((height)*(line_size));
	
	Group=(unsigned short*)malloc(H*a);
	GroupTemp=(unsigned short*) Group;
	pBufTemp=(unsigned char*) pBuf;
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			*(pBufTemp+w+h*line_size) = *(pTemp+w+h*line_size);
		}
	}
	
	//iPhonePhotoGrayFilter1((unsigned char*)pBuf,width,height);
	iPhonePhoto8bitColor((unsigned char*)pBuf,width,height);
	iPhonePhotoGrayFilter1((unsigned char*)pBuf,width,height,50);
	//iPhonePhotoMedianFilter5x5G((unsigned char*)pBuf,width,height);
	temp=0;
	///////그룹화/////////////////////////////////
	///////그룹화////////////////////////////////
	///////그룹화////////////////////////////////
	///////그룹화////////////////////////////////
	///////그룹화////////////////////////////////
	for(int h=0; h<(H); h++)
	{
		for(int w=0; w<(W); w++)
		{
			//pBuf[h+2][w+8] 
			*(Group+(h)*width+w)= 0;
		}
	}
	
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	for(int h=0; h<(H); h++)
	{
		for(int w=0; w<(W); w++)
		{
			if(h>0&&(*(pBufTemp)==*(pBufTemp-line_size))){
				*(GroupTemp)=*(GroupTemp-W);
				
				
				if(w>0&&(*(pBufTemp)==*(pBufTemp-4))&&*(GroupTemp)!=*(GroupTemp-1)){
					for(int i=0; i<=h; i++){
						for(int j=0; j<W; j++){
							if(*(Group+i*W+j)==*(GroupTemp-1)) *(Group+i*W+j)=*(GroupTemp);
						}
					}
				}
				
				
			}
			else if(w>0&&(*(pBufTemp)==*(pBufTemp-4)))*(GroupTemp)=*(GroupTemp-1);
			
			else if(h>0&&w<(width-1)&&(*(pBufTemp)==*(pBufTemp-line_size+4))){
				*(GroupTemp)=*(GroupTemp-W+1);
				
			}
			
			else *(GroupTemp)=temp++;
			pBufTemp+=4;
			GroupTemp++;
			
		}
	}
	
	pTemp=pSrc;
	GroupTemp=(unsigned short*) Group;
	chkTemp=(unsigned short*)malloc(sizeof(unsigned short)*temp);
	//chkTemp1=(unsigned short*)malloc(sizeof(unsigned short)*temp);
	//if(w>0&&(*(pTemp)==*(pTemp-4))&&*(pBufTemp)!=*(pBufTemp-1))
	for(int x=0;x<temp;x++)*(chkTemp+x)=0;
	for(int h=0; h<(H); h++)
	{
		for(int w=0; w<(W); w++){
			*(chkTemp+*(GroupTemp))+=1;
			GroupTemp++;
		}
	}
	cnt=0;
	for(int x=0;x<temp;x++)if(*(chkTemp+x)>0)*(chkTemp+x)=cnt++;
	GroupTemp=(unsigned short*) Group;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width); w++)
		{
			*(GroupTemp)=*(chkTemp+*(GroupTemp));
			GroupTemp++;
		}
	}
	
	
	
	////
	
	
	
	GroupTemp=(unsigned short*) Group;
	/*
	 for(int x=0;x<temp;x++)chkTemp1[x]=0;
	 
	 
	 for(int h=0; h<(H); h++)
	 {
	 for(int w=0; w<(W); w++){
	 if(w>0&&(*(pTemp)==*(pTemp-4))&&*(pBufTemp)!=*(pBufTemp-1)){
	 if(chkTemp1[*(GroupTemp-1)]==0||chkTemp1[*(GroupTemp-1)]>*(GroupTemp))chkTemp1[*(GroupTemp-1)]=*(GroupTemp);
	 
	 //else if(chkTemp1[*(pBufTemp-1)]==0)chkTemp1[*(pBufTemp-1)]=*(pBufTemp);
	 
	 //(chkTemp1+*(pBufTemp-1))=*(pBufTemp);
	 //NSLog(@"%d",)
	 }
	 
	 
	 GroupTemp++;
	 pTemp+=4;
	 
	 
	 }
	 }
	 */
	//	unsigned short tt[temp];
	//	for(int i=1;i<temp;i++){tt[i]=chkTemp1[i];}
	//
	
	

	/*
	 for(int i=1;i<temp;i++){
	 if(chkTemp1[i]){
	 index = i;
	 count = 0;
	 do{
	 count++;
	 index = chkTemp1[index];
	 }while(chkTemp1[index]);
	 chkTemp1[i] = index;
	 }
	 }*/
	
	
	
	pTemp=pSrc;
	GroupTemp=(unsigned short*) Group;
	
	for(int h=0; h<(H); h++)
	{
		for(int w=0; w<(W); w++){
			//if(chkTemp1[*(GroupTemp-1)])*(GroupTemp)=chkTemp1[*(GroupTemp)];
			
			GroupTemp++;
			
			
		}
	}
	
	
	////////////////그룹화////////////////////////////////
	////////////////그룹화////////////////////////////////
	////////////////그룹화////////////////////////////////
	////////////////그룹화////////////////////////////////
	
	pTemp=pSrc;
	GroupTemp=(unsigned short*) Group;
	GroupR=(unsigned int*)malloc(sizeof(unsigned int)*cnt);
	GroupG=(unsigned int*)malloc(sizeof(unsigned int)*cnt);
	GroupB=(unsigned int*)malloc(sizeof(unsigned int)*cnt);
	pixNum=(unsigned int*)malloc(sizeof(unsigned int)*cnt);
	
	for(short x=0;x<cnt;x++){
		GroupR[x]=0;
		GroupG[x]=0;
		GroupB[x]=0;
		pixNum[x]=0;
	}
	
	for(int h=0; h<(H); h++)
	{
		for(int w=0; w<(W); w++)
		{
			
			
			GroupR[*(Group+h*W+w)]+=*(pTemp+h*line_size+w*4);
			GroupG[*(Group+h*W+w)]+=*(pTemp+h*line_size+w*4+1);
			GroupB[*(Group+h*W+w)]+=*(pTemp+h*line_size+w*4+2);
			pixNum[*(Group+h*W+w)]++;
			
		}
	}
	for(short x=0;x<cnt;x++){
		if(pixNum[x]){
			GroupR[x]/=pixNum[x];
			GroupG[x]/=pixNum[x];
			GroupB[x]/=pixNum[x];
		}
	}
	for(int h=0; h<(H); h++)
	{
		for(int w=0; w<(W); w++)
		{
			*(pTemp+h*line_size+w*4)=GroupR[*(Group+h*W+w)];
			*(pTemp+h*line_size+w*4+1)=GroupG[*(Group+h*W+w)];
			*(pTemp+h*line_size+w*4+2)=GroupB[*(Group+h*W+w)];
			
		}
	}
	
	
	
	/*
	 for(int h=0; h<(H); h++)
	 {
	 for(int w=0; w<(W); w++)
	 {
	 
	 *(pTemp+h*line_size+w*4)=*(Group+h*W+w);
	 *(pTemp+h*line_size+w*4+1)=*(Group+h*W+w);
	 *(pTemp+h*line_size+w*4+2)=*(Group+h*W+w);
	 
	 }
	 }
	 */
	if(Group!=NULL)free(Group);
	if(pBuf!=NULL)free(pBuf);
	if(GroupR!=NULL)free(GroupR);
	if(GroupG!=NULL)free(GroupG);
	
	if(GroupB!=NULL)free(GroupB);
	if(pixNum!=NULL)free(pixNum);
	
	if(chkTemp!=NULL)free(chkTemp);
	
	
}

void iPhonePhotoMotionRoughFilter(unsigned char* pSrc,unsigned int width,unsigned int height){
	unsigned int line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;//[height+4][width*4+16];
	int pixelR,pixelG,pixelB;
	int random;
	buf_line_size=width*4+40;
	pBuf=(unsigned char*)malloc((height)*buf_line_size);
	line_size=width*4;
	
	
	
	
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	


	/*
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<80; w++)
		{
			//pBuf[h+2][w] 
			*(pBuf+(h)*buf_line_size+w)= *(pTemp+w+h*line_size);
		}
	}*/
	

	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4+40); w++)
		{
			//pBuf[h+2][w+8] 
			if(w<width*4)*(pBuf+h*buf_line_size+w)= *(pTemp+w+h*line_size);
			else *(pBuf+h*buf_line_size+w)= *(pTemp+w+h*line_size-40);
			
		}
	}

	
	
	
	
	//pTemp+=line_size*2+8;
	for(int h=0; h<height; h++)
	{
		for(int w=0; w<width; w++)
		{
			
			random=rand()%4;
			
 			pixelR = 0;
			pixelG = 0;
			pixelB = 0;
			// line - 1
			if(random==0){
				pixelR += *(pBufTemp+40);
				pixelG += *(pBufTemp+41);
				pixelB += *(pBufTemp+42);
			}
			if(random==1){
				pixelR += *(pBufTemp+36);
				pixelG += *(pBufTemp+37);
				pixelB += *(pBufTemp+38);
			}
			if(random==2){
				pixelR += *(pBufTemp+32);
				pixelG += *(pBufTemp+33);
				pixelB += *(pBufTemp+34);
			}
			if(random==3){
				pixelR += *(pBufTemp+28);
				pixelG += *(pBufTemp+29);
				pixelB += *(pBufTemp+30);
			}
			
			
			if(pixelR<0) pixelR *= (-1);
			if(pixelG<0) pixelG *= (-1);
			if(pixelB<0) pixelB *= (-1);
			
			//total += sobel;
			
			*(pTemp+0) = pixelR;
			*(pTemp+1) = pixelG;
			*(pTemp+2) = pixelB;
			
			if(random%2){
				if(h%5==3||h%5==4){
					*(pTemp+0) = 128;
					*(pTemp+1) = 128;
					*(pTemp+2) = 128;
				}
			}
			 
			pBufTemp +=4;
			pTemp += 4;
		}
		//pTemp +=16 ;
		pBufTemp += 40;
	} 
	if(pBuf!=NULL)free(pBuf);
	return;
}
void iPhonePhotoFilterLV07(unsigned char* pSrc,unsigned int width,unsigned int height){
	unsigned int line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;
	unsigned char* pBuf2;
	//[height][width*4];
	line_size=width*4;
	
	pBuf=(unsigned char*)malloc(height*line_size);
	pBuf2=(unsigned char*)malloc(height*line_size);
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			*(pBuf+w+h*line_size)= *(pTemp+w+h*line_size);
			*(pBuf2+w+h*line_size)= *(pTemp+w+h*line_size);
		}
	}
	
	int x,y;
	int xx,yy;
	
	
	int vv,vr,vg,vb;
	int ave,rr,gg,bb;
	
	
	
	int sadr;
	int nn,i;
	int x1,y1,x2,y2;
	
	x1=3;
	y1=3;
	x2=width-3;
	y2=height-3;
	
	
	setRegInfo();
	nn=2;
	for(y=y1;y<y2;y++) {
		for(x=x1;x<=x2;x++) {
			rr=gg=bb=0;
			sadr=0;
			for(yy=(-nn);yy<=nn;yy++) {
				for(xx=(-nn);xx<=nn;xx++) {
					if(((xx+x)>0&&(xx+x)<x2)&&((yy+y)>0&&(yy+y)<y2)){

					imgR[sadr]=*(pTemp+(xx+x)*4+(yy+y)*line_size);
					imgG[sadr]=*(pTemp+(xx+x)*4+(yy+y)*line_size+1);
					imgB[sadr]=*(pTemp+(xx+x)*4+(yy+y)*line_size+2); 
					sadr++;
					}
				}
			}
			vr=vg=vb= 1000;
			for(i=0;i<=8;i++) {                     // ev[gÌàCÅåÅáÌ·ªÅ¬ÉÈéàÌðIÔ
				vv=getRegVal(imgR,i,&ave);
				if(vr>vv) {
					vr=vv;
					rr=ave;
				}
				vv=getRegVal(imgG,i,&ave);
				if(vg>vv) {
					vg=vv;
					gg=ave;
				}
				vv=getRegVal(imgB,i,&ave);
				if(vb>vv) {
					vb=vv;
					bb=ave;
				}
			}
			*(pBuf2+x*4+y*line_size)=rr;

			
			*(pBuf2+x*4+y*line_size+1)=gg;

			
			*(pBuf2+x*4+y*line_size+2)=bb;
// æÉlðZbg·é
		}
	} 	/*모자이크
	 
	 int val;
	 int x,y;
	 int xx,yy;
	 int sx,sy;
	 int ex,ey;
	 int bx,by;
	 int rr,gg,bb;
	 int dum,dum2;
	 int endn;
	 int mm;
	 int mlx,mly,zzx;
	 int my1,my2;
	 int col[3];
	 int x1,y1,x2,y2;
	 int msx,msy;
	 x1=0;
	 y1=0;
	 x2=width-1;
	 y2=height-1;
	 msx=10;
	 msy=10;
	 if(msx==0) msx=1;
	 if(msy==0) msy=1;
	 mlx=(x2-x1+1+msx-1)/msx;
	 mly=(y2-y1+1+msy-1)/msy;
	 zzx=msx/2;
	 for(y=0;y<mly;y++) {
	 bx=0;
	 for(x=bx;x<x2;x+=msx) {
	 rr=gg=bb=0;
	 sx=x;
	 if(x<0) sx=0;
	 sy=y1+y*msy;
	 ex=x+msx-1;
	 ey=sy+msy-1;
	 if(ex>x2) ex=x2;
	 if(ey>y2) ey=y2;
	 for(yy=sy;yy<=ey;yy++) {
	 for(xx=sx;xx<=ex;xx++) {
	 val =*(pBuf+xx*4+yy*line_size);//getPixel(img,xx,yy,&col); //æãÌæfîñðæŸ
	 rr+=*(pBuf+xx*4+yy*line_size);
	 gg+=*(pBuf+xx*4+yy*line_size+1);
	 bb+=*(pBuf+xx*4+yy*line_size+2);
	 }
	 }
	 mm=(ex-sx+1)*(ey-sy+1);
	 if(mm) {
	 rr/=mm;
	 gg/=mm;
	 bb/=mm;
	 col[0] = rr;
	 col[1] = gg;
	 col[2] = bb;
	 for(yy=sy;yy<=ey;yy++) {
	 for(xx=sx;xx<=ex;xx++) {
	 *(pBuf2+xx*4+yy*line_size)=col[0];
	 *(pBuf2+xx*4+yy*line_size+1)=col[1];
	 *(pBuf2+xx*4+yy*line_size+2)=col[2];    // æÉlðZbg·é
	 }
	 }
	 }
	 }
	 } */
	
	/*
	 iPhonePhotoOverlayFilter((unsigned char*)pBuf,width,height);
	 iPhonePhotoOverlayFilter((unsigned char*)pBuf2,width,height);
	 iPhonePhotoOverlayFilter((unsigned char*)pBuf2,width,height);
	 
	 //iPhonePhotoInvertFilter((unsigned char*)pBuf,width,height);
	 
	 
	 
	 pTemp=pSrc;
	 for(int h=0; h<(height); h++)
	 {
	 for(int w=0; w<(width*4); w+=4)
	 
	 
	 {
	 
	 if(rand()%(h%5*h%5+1)==0){
	 if((w+h*10)%100>90&&rand()%3){						
	 if(w<12){
	 *(pTemp+w+h*line_size) =*(pBuf+w+h*line_size);
	 *(pTemp+w+h*line_size+1) =*(pBuf+w+h*line_size+1);
	 *(pTemp+w+h*line_size+2) =*(pBuf+w+h*line_size+2);
	 }
	 else if(w<20){
	 *(pTemp+w+h*line_size) =*(pBuf+w+h*line_size-8);
	 *(pTemp+w+h*line_size+1) =*(pBuf+w+h*line_size+1-8);
	 *(pTemp+w+h*line_size+2) =*(pBuf+w+h*line_size+2-8);
	 }
	 else if(w<28){
	 *(pTemp+w+h*line_size) =*(pBuf+w+h*line_size-16);
	 *(pTemp+w+h*line_size+1) =*(pBuf+w+h*line_size+1-16);
	 *(pTemp+w+h*line_size+2) =*(pBuf+w+h*line_size+2-16);
	 }
	 else if(w<36){
	 *(pTemp+w+h*line_size) =*(pBuf+w+h*line_size-24);
	 *(pTemp+w+h*line_size+1) =*(pBuf+w+h*line_size+1-24);
	 *(pTemp+w+h*line_size+2) =*(pBuf+w+h*line_size+2-24);
	 }
	 else{
	 *(pTemp+w+h*line_size) =*(pBuf+w+h*line_size-32);
	 *(pTemp+w+h*line_size+1) =*(pBuf+w+h*line_size-32+1);
	 *(pTemp+w+h*line_size+2) =*(pBuf+w+h*line_size-32+2);
	 }
	 }else{
	 if(w<12){
	 *(pTemp+w+h*line_size) =*(pBuf2+w+h*line_size);
	 *(pTemp+w+h*line_size+1) =*(pBuf2+w+h*line_size+1);
	 *(pTemp+w+h*line_size+2) =*(pBuf2+w+h*line_size+2);
	 }
	 else if(w<20){
	 *(pTemp+w+h*line_size) =*(pBuf2+w+h*line_size-8);
	 *(pTemp+w+h*line_size+1) =*(pBuf2+w+h*line_size+1-8);
	 *(pTemp+w+h*line_size+2) =*(pBuf2+w+h*line_size+2-8);
	 }
	 else if(w<28){
	 *(pTemp+w+h*line_size) =*(pBuf2+w+h*line_size-16);
	 *(pTemp+w+h*line_size+1) =*(pBuf2+w+h*line_size+1-16);
	 *(pTemp+w+h*line_size+2) =*(pBuf2+w+h*line_size+2-16);
	 }
	 else if(w<36){
	 *(pTemp+w+h*line_size) =*(pBuf2+w+h*line_size-24);
	 *(pTemp+w+h*line_size+1) =*(pBuf2+w+h*line_size+1-24);
	 *(pTemp+w+h*line_size+2) =*(pBuf2+w+h*line_size+2-24);
	 }
	 else{
	 *(pTemp+w+h*line_size) =*(pBuf2+w+h*line_size-32);
	 *(pTemp+w+h*line_size+1) =*(pBuf2+w+h*line_size-32+1);
	 *(pTemp+w+h*line_size+2) =*(pBuf2+w+h*line_size-32+2);
	 }
	 
	 
	 
	 
	 }
	 
	 }		
	 else{
	 if((w+h)%100<90&&rand()%3){						
	 *(pTemp+w+h*line_size) =*(pBuf+w+h*line_size);
	 *(pTemp+w+h*line_size+1) =*(pBuf+w+h*line_size+1);
	 *(pTemp+w+h*line_size+2) =*(pBuf+w+h*line_size+2);
	 }
	 
	 
	 }	
	 
	 
	 
	 
	 }
	 }*/
	
	
	pTemp=pSrc;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			*(pTemp+w+h*line_size) =*(pBuf2+w+h*line_size);
		}
	}
	if(pBuf!=NULL)free(pBuf);
	if(pBuf2!=NULL)free(pBuf2);
	
}
void iPhonePhotoFilterLV06(unsigned char* pSrc,unsigned int width,unsigned int height){
	unsigned int line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;
	unsigned char* pBuf2;
	//[height][width*4];
	line_size=width*4;


	pBuf2=(unsigned char*)malloc(height*line_size);
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;

	
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			*(pBuf2+w+h*line_size)= *(pTemp+w+h*line_size);
			
		}
	}

	int x,y;
	int xx,yy;


	int vv,vr,vg,vb;
	int ave,rr,gg,bb;



	int sadr;
	int nn,i;
	int x1,y1,x2,y2;
	
	x1=2;
	y1=2;
	x2=width/2-1;
	y2=height/2-1;
	
	
	setRegInfo();
	nn=2;
	for(y=y1;y<y2;y++) {
		for(x=x1;x<=x2;x++) {
			rr=gg=bb=0;
			sadr=0;
			for(yy=(-nn);yy<=nn;yy++) {
				for(xx=(-nn);xx<=nn;xx++) {
		 //æãÌæfîñðæŸ
					if(((xx+x)>0&&(xx+x)<x2)&&((yy+y)>0&&(yy+y)<y2)){
						imgR[sadr]=*(pTemp+(xx+x)*8+(yy+y)*line_size*2);
						imgG[sadr]=*(pTemp+(xx+x)*8+(yy+y)*line_size*2+1);
						imgB[sadr]=*(pTemp+(xx+x)*8+(yy+y)*line_size*2+2); 
						sadr++;
					}
				}
			}
			vr=vg=vb= 1000;
			for(i=0;i<=8;i++) {                     // ev[gÌàCÅåÅáÌ·ªÅ¬ÉÈéàÌðIÔ
				vv=getRegVal(imgR,i,&ave);
				if(vr>vv) {
					vr=vv;
					rr=ave;
				}
				vv=getRegVal(imgG,i,&ave);
				if(vg>vv) {
					vg=vv;
					gg=ave;
				}
				vv=getRegVal(imgB,i,&ave);
				if(vb>vv) {
					vb=vv;
					bb=ave;
				}
			}
			*(pBuf2+x*8+y*line_size*2)=rr;
			*(pBuf2+x*8+4+y*line_size*2)=rr;
			*(pBuf2+x*8+y*line_size*2+line_size)=rr;
			*(pBuf2+x*8+4+y*line_size*2+line_size)=rr;
	
			*(pBuf2+x*8+y*line_size*2+1)=gg;
			*(pBuf2+x*8+4+y*line_size*2+1)=gg;
			*(pBuf2+x*8+y*line_size*2+line_size+1)=gg;
			*(pBuf2+x*8+4+y*line_size*2+line_size+1)=gg;

			*(pBuf2+x*8+y*line_size*2+2)=bb;
			*(pBuf2+x*8+4+y*line_size*2+2)=bb;
			*(pBuf2+x*8+y*line_size*2+line_size+2)=bb;
			*(pBuf2+x*8+4+y*line_size*2+line_size+2)=bb;// æÉlðZbg·é
		}
	} 	/*모자이크
	
	int val;
	int x,y;
	int xx,yy;
	int sx,sy;
	int ex,ey;
	int bx,by;
	int rr,gg,bb;
	int dum,dum2;
	int endn;
	int mm;
	int mlx,mly,zzx;
	int my1,my2;
	int col[3];
	int x1,y1,x2,y2;
	int msx,msy;
	x1=0;
	y1=0;
	x2=width-1;
	y2=height-1;
	msx=10;
	msy=10;
	if(msx==0) msx=1;
	if(msy==0) msy=1;
	mlx=(x2-x1+1+msx-1)/msx;
	mly=(y2-y1+1+msy-1)/msy;
	zzx=msx/2;
	for(y=0;y<mly;y++) {
		bx=0;
		for(x=bx;x<x2;x+=msx) {
			rr=gg=bb=0;
			sx=x;
			if(x<0) sx=0;
			sy=y1+y*msy;
			ex=x+msx-1;
			ey=sy+msy-1;
			if(ex>x2) ex=x2;
			if(ey>y2) ey=y2;
			for(yy=sy;yy<=ey;yy++) {
				for(xx=sx;xx<=ex;xx++) {
					val =*(pBuf+xx*4+yy*line_size);//getPixel(img,xx,yy,&col); //æãÌæfîñðæŸ
					rr+=*(pBuf+xx*4+yy*line_size);
					gg+=*(pBuf+xx*4+yy*line_size+1);
					bb+=*(pBuf+xx*4+yy*line_size+2);
				}
			}
			mm=(ex-sx+1)*(ey-sy+1);
			if(mm) {
				rr/=mm;
				gg/=mm;
				bb/=mm;
				col[0] = rr;
				col[1] = gg;
				col[2] = bb;
				for(yy=sy;yy<=ey;yy++) {
					for(xx=sx;xx<=ex;xx++) {
						*(pBuf2+xx*4+yy*line_size)=col[0];
						*(pBuf2+xx*4+yy*line_size+1)=col[1];
						*(pBuf2+xx*4+yy*line_size+2)=col[2];    // æÉlðZbg·é
					}
				}
			}
		}
	} */
	
				/*
	iPhonePhotoOverlayFilter((unsigned char*)pBuf,width,height);
	iPhonePhotoOverlayFilter((unsigned char*)pBuf2,width,height);
	iPhonePhotoOverlayFilter((unsigned char*)pBuf2,width,height);

	//iPhonePhotoInvertFilter((unsigned char*)pBuf,width,height);
	
	
	
	pTemp=pSrc;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w+=4)
			
			
		{
			
			if(rand()%(h%5*h%5+1)==0){
				if((w+h*10)%100>90&&rand()%3){						
					if(w<12){
						*(pTemp+w+h*line_size) =*(pBuf+w+h*line_size);
						*(pTemp+w+h*line_size+1) =*(pBuf+w+h*line_size+1);
						*(pTemp+w+h*line_size+2) =*(pBuf+w+h*line_size+2);
					}
					else if(w<20){
						*(pTemp+w+h*line_size) =*(pBuf+w+h*line_size-8);
						*(pTemp+w+h*line_size+1) =*(pBuf+w+h*line_size+1-8);
						*(pTemp+w+h*line_size+2) =*(pBuf+w+h*line_size+2-8);
					}
					else if(w<28){
						*(pTemp+w+h*line_size) =*(pBuf+w+h*line_size-16);
						*(pTemp+w+h*line_size+1) =*(pBuf+w+h*line_size+1-16);
						*(pTemp+w+h*line_size+2) =*(pBuf+w+h*line_size+2-16);
					}
					else if(w<36){
						*(pTemp+w+h*line_size) =*(pBuf+w+h*line_size-24);
						*(pTemp+w+h*line_size+1) =*(pBuf+w+h*line_size+1-24);
						*(pTemp+w+h*line_size+2) =*(pBuf+w+h*line_size+2-24);
					}
					else{
						*(pTemp+w+h*line_size) =*(pBuf+w+h*line_size-32);
						*(pTemp+w+h*line_size+1) =*(pBuf+w+h*line_size-32+1);
						*(pTemp+w+h*line_size+2) =*(pBuf+w+h*line_size-32+2);
					}
				}else{
					if(w<12){
						*(pTemp+w+h*line_size) =*(pBuf2+w+h*line_size);
						*(pTemp+w+h*line_size+1) =*(pBuf2+w+h*line_size+1);
						*(pTemp+w+h*line_size+2) =*(pBuf2+w+h*line_size+2);
					}
					else if(w<20){
						*(pTemp+w+h*line_size) =*(pBuf2+w+h*line_size-8);
						*(pTemp+w+h*line_size+1) =*(pBuf2+w+h*line_size+1-8);
						*(pTemp+w+h*line_size+2) =*(pBuf2+w+h*line_size+2-8);
					}
					else if(w<28){
						*(pTemp+w+h*line_size) =*(pBuf2+w+h*line_size-16);
						*(pTemp+w+h*line_size+1) =*(pBuf2+w+h*line_size+1-16);
						*(pTemp+w+h*line_size+2) =*(pBuf2+w+h*line_size+2-16);
					}
					else if(w<36){
						*(pTemp+w+h*line_size) =*(pBuf2+w+h*line_size-24);
						*(pTemp+w+h*line_size+1) =*(pBuf2+w+h*line_size+1-24);
						*(pTemp+w+h*line_size+2) =*(pBuf2+w+h*line_size+2-24);
					}
					else{
						*(pTemp+w+h*line_size) =*(pBuf2+w+h*line_size-32);
						*(pTemp+w+h*line_size+1) =*(pBuf2+w+h*line_size-32+1);
						*(pTemp+w+h*line_size+2) =*(pBuf2+w+h*line_size-32+2);
					}
				
			
					
					
				}
			
			}		
			else{
				if((w+h)%100<90&&rand()%3){						
					*(pTemp+w+h*line_size) =*(pBuf+w+h*line_size);
					*(pTemp+w+h*line_size+1) =*(pBuf+w+h*line_size+1);
					*(pTemp+w+h*line_size+2) =*(pBuf+w+h*line_size+2);
				}
				
				
			}	
		
		
			 
			
		}
	}*/

	
	pTemp=pSrc;
	/*
	for(int h=0; h<(height)/2-1; h++)
	{
		for(int w=0; w<(width)/2-1; w++)
		{
			rr=gg=bb=0;
			*(pBuf2+w*8+4+h*line_size*2)=(*(pBuf2+w*8+h*line_size*2)+*(pBuf2+w*8+8+h*line_size*2))/2;
			*(pBuf2+w*8+h*line_size*2+line_size)=(*(pBuf2+w*8+h*line_size*2)+*(pBuf2+w*8+h*line_size*2+line_size*2))/2;
			*(pBuf2+w*8+4+h*line_size*2+1)=(*(pBuf2+w*8+h*line_size*2+1)+*(pBuf2+w*8+8+h*line_size*2+1))/2;
			*(pBuf2+w*8+h*line_size*2+line_size+1)=(*(pBuf2+w*8+h*line_size*2+1)+*(pBuf2+w*8+h*line_size*2+line_size*2+1))/2;
			*(pBuf2+w*8+4+h*line_size*2+2)=(*(pBuf2+w*8+h*line_size*2+2)+*(pBuf2+w*8+8+h*line_size*2+2))/2;
			*(pBuf2+w*8+h*line_size*2+line_size+2)=(*(pBuf2+w*8+h*line_size*2+2)+*(pBuf2+w*8+h*line_size*2+line_size*2+2))/2;
			//*(pBuf2+w*8+4+h*line_size*2)=(*(pBuf2+w*8+h*line_size*2)+*(pBuf2+w*8+8+h*line_size*2))/2;
			rr+=*(pBuf2+w*8+h*line_size*2);
			rr+=*(pBuf2+w*8+4+h*line_size*2);
			rr+=*(pBuf2+w*8+8+h*line_size*2);
			rr+=*(pBuf2+w*8+h*line_size*2+line_size);
			rr+=*(pBuf2+w*8+8+h*line_size*2+line_size);
			rr+=*(pBuf2+w*8+h*line_size*2+line_size*2);
			rr+=*(pBuf2+w*8+4+h*line_size*2+line_size*2);
			rr+=*(pBuf2+w*8+8+h*line_size*2+line_size*2);
			*(pBuf2+w*8+4+h*line_size*2+line_size)=rr/8;
			
			gg+=*(pBuf2+w*8+h*line_size*2+1);
			gg+=*(pBuf2+w*8+4+h*line_size*2+1);
			gg+=*(pBuf2+w*8+8+h*line_size*2+1);
			gg+=*(pBuf2+w*8+h*line_size*2+line_size+1);
			gg+=*(pBuf2+w*8+8+h*line_size*2+line_size+1);
			gg+=*(pBuf2+w*8+h*line_size*2+line_size*2+1);
			gg+=*(pBuf2+w*8+4+h*line_size*2+line_size*2+1);
			gg+=*(pBuf2+w*8+8+h*line_size*2+line_size*2+1);
			*(pBuf2+w*8+4+h*line_size*2+line_size+1)=gg/8;
			
			bb+=*(pBuf2+w*8+h*line_size*2+2);
			bb+=*(pBuf2+w*8+4+h*line_size*2+2);
			bb+=*(pBuf2+w*8+8+h*line_size*2+2);
			bb+=*(pBuf2+w*8+h*line_size*2+line_size+2);
			bb+=*(pBuf2+w*8+8+h*line_size*2+line_size+2);
			bb+=*(pBuf2+w*8+h*line_size*2+line_size*2+2);
			bb+=*(pBuf2+w*8+4+h*line_size*2+line_size*2+2);
			bb+=*(pBuf2+w*8+8+h*line_size*2+line_size*2+2);
			*(pBuf2+w*8+4+h*line_size*2+line_size+2)=bb/8;
			
		}
	}*/
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			*(pTemp+w+h*line_size) =*(pBuf2+w+h*line_size);
		}
	}
	if(pBuf2!=NULL)free(pBuf2);
	
}

void iPhonePhotoFilterLV0(unsigned char* pSrc,unsigned int width,unsigned int height){
	unsigned int line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;//[height][width*4];
	line_size=width*4;
	unsigned char temp;
	pBuf=(unsigned char*)malloc(height*line_size);
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			*(pBuf+w+h*line_size)= *(pTemp+w+h*line_size);
		}
	}
		iPhonePhotoGrayFilter1((unsigned char*)pBuf,width,height,50);
	iPhonePhotoGaussianFilter5x5_x((unsigned char*)pBuf,width,height);
	//iPhonePhotoBinaryFilterGray2((unsigned char*)pBuf,width,height);
	iPhonePhotoLaplace((unsigned char*)pBuf,width,height);
	iPhonePhotoPrewittFilter1((unsigned char*)pTemp,width,height);
		iPhonePhotoInvertFilter((unsigned char*)pTemp,width,height);
	iPhonePhotoInvertFilter((unsigned char*)pBuf,width,height);
	
	
	pTemp=pSrc;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			
			
			*(pTemp+w+h*line_size)=  *(pBuf+w+h*line_size);//*(*(pTemp+w+h*line_size))/255;
			
		}
	}
	//iPhonePhotoInvertFilter((unsigned char*)pTemp,width,height);
	//iPhonePhotoMotionBlurFilter((unsigned char*)pBuf,width,height);
	
	

	if(pBuf!=NULL)free(pBuf);
	
	
}

void iPhonePhotoLaplace(unsigned char* pSrc,unsigned int width,unsigned int height){
	
	unsigned int line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;//[height+4][width*4+16];
	int pixelR,pixelG,pixelB;
	line_size=width*4;
	buf_line_size=width*4+16;
	pBuf=(unsigned char*)malloc(buf_line_size*(height+4));
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//			pBuf[h][w+8]
			*(pBuf+h*buf_line_size+w+8)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//			pBuf[h+height+2][w+8]
			*(pBuf+(h+height+2)*buf_line_size+w+8)= *(pTemp+w+(height-2+h)*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			//pBuf[h+2][w] 
			*(pBuf+(h+2)*buf_line_size+w)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			//	pBuf[h+2][w+8+width*4]
			*(pBuf+(h+2)*buf_line_size+w+8+width*4)= *(pTemp+(width-2)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//pBuf[h+2][w+8] 
			*(pBuf+(h+2)*buf_line_size+w+8)= *(pTemp+w+h*line_size);
		}
	}
	
	
	//pTemp+=line_size*2+8;
	for(int h=0; h<height; h++)
	{
		for(int w=0; w<width; w++)
		{
			
 			pixelR = 0;
			pixelG = 0;
			pixelB = 0;
			// line - 1
			pixelR -= *pBufTemp;
			pixelG -= *(pBufTemp+1);
			pixelB -= *(pBufTemp+2);
		  	pBufTemp += 4;
			pixelR -= *pBufTemp;
			pixelG -= *(pBufTemp+1);
			pixelB -= *(pBufTemp+2);
			pBufTemp += 4;
			pixelR -= *pBufTemp;
			pixelG -= *(pBufTemp+1);
			pixelB -= *(pBufTemp+2);
			pBufTemp += 4;
			pixelR -= *pBufTemp;
			pixelG -= *(pBufTemp+1);
			pixelB -= *(pBufTemp+2);
			pBufTemp += 4;
			pixelR -= *pBufTemp;
			pixelG -= *(pBufTemp+1);
			pixelB -= *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 2
			pixelR -= *pBufTemp;
			pixelG -= *(pBufTemp+1);
			pixelB -= *(pBufTemp+2);
		  	pBufTemp += 4;
			pixelR -= *pBufTemp;
			pixelG -= *(pBufTemp+1);
			pixelB -= *(pBufTemp+2);
			pBufTemp += 4;
			pixelR -= *pBufTemp;
			pixelG -= *(pBufTemp+1);
			pixelB -= *(pBufTemp+2);
			pBufTemp += 4;
			pixelR -= *pBufTemp;
			pixelG -= *(pBufTemp+1);
			pixelB -= *(pBufTemp+2);
			pBufTemp += 4;
			pixelR -= *pBufTemp;
			pixelG -= *(pBufTemp+1);
			pixelB -= *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 3
			pixelR -= *pBufTemp;
			pixelG -= *(pBufTemp+1);
			pixelB -= *(pBufTemp+2);
		  	pBufTemp += 4;
			pixelR -= *pBufTemp;
			pixelG -= *(pBufTemp+1);
			pixelB -= *(pBufTemp+2);
			pBufTemp += 4;
			pixelR += 24*(*pBufTemp);
			pixelG += 24*(*(pBufTemp+1));
			pixelB += 24*(*(pBufTemp+2));
			pBufTemp += 4;
			pixelR -= *pBufTemp;
			pixelG -= *(pBufTemp+1);
			pixelB -= *(pBufTemp+2);
			pBufTemp += 4;
			pixelR -= *pBufTemp;
			pixelG -= *(pBufTemp+1);
			pixelB -= *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 4
			pixelR -= *pBufTemp;
			pixelG -= *(pBufTemp+1);
			pixelB -= *(pBufTemp+2);
		  	pBufTemp += 4;
			pixelR -= *pBufTemp;
			pixelG -= *(pBufTemp+1);
			pixelB -= *(pBufTemp+2);
			pBufTemp += 4;
			pixelR -= *pBufTemp;
			pixelG -= *(pBufTemp+1);
			pixelB -= *(pBufTemp+2);
			pBufTemp += 4;
			pixelR -= *pBufTemp;
			pixelG -= *(pBufTemp+1);
			pixelB -= *(pBufTemp+2);
			pBufTemp += 4;
			pixelR -= *pBufTemp;
			pixelG -= *(pBufTemp+1);
			pixelB -= *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 16;
			// line - 5
			pixelR -= *pBufTemp;
			pixelG -= *(pBufTemp+1);
			pixelB -= *(pBufTemp+2);
		  	pBufTemp += 4;
			pixelR -= *pBufTemp;
			pixelG -= *(pBufTemp+1);
			pixelB -= *(pBufTemp+2);
			pBufTemp += 4;
			pixelR -= *pBufTemp;
			pixelG -= *(pBufTemp+1);
			pixelB -= *(pBufTemp+2);
			pBufTemp += 4;
			pixelR -= *pBufTemp;
			pixelG -= *(pBufTemp+1);
			pixelB -= *(pBufTemp+2);
			pBufTemp += 4;
			pixelR -= *pBufTemp;
			pixelG -= *(pBufTemp+1);
			pixelB -= *(pBufTemp+2);
			
			
			
			pBufTemp = pBufTemp - (buf_line_size << 2) - 12;
			
			
			if(pixelR<0) pixelR =0;
			else if(pixelR>255) pixelR =255;
			if(pixelG<0) pixelG = 0;
			else if(pixelG>255) pixelG =255;
			if(pixelB<0) pixelB = 0;
			else if(pixelB>255) pixelB =255;

			//total += sobel;
			
			*(pTemp+0) = pixelR;
			*(pTemp+1) = pixelG;
			*(pTemp+2) = pixelB;
			pTemp += 4;
		}
		//pTemp +=16 ;
		pBufTemp += 16;
	}
	if(pBuf!=NULL)free(pBuf);
	return;
	
}
void iPhonePhotoMosaic(unsigned char* pSrc,unsigned int width,unsigned int height,unsigned int value){
	
	unsigned int line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;//[height+4][width*4+16];
	int pixelR,pixelG,pixelB;
	line_size=width*4;
	buf_line_size=width*4+16;
	pBuf=(unsigned char*)malloc(buf_line_size*(height+4));
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//			pBuf[h][w+8]
			*(pBuf+h*buf_line_size+w+8)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//			pBuf[h+height+2][w+8]
			*(pBuf+(h+height+2)*buf_line_size+w+8)= *(pTemp+w+(height-2+h)*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			//pBuf[h+2][w] 
			*(pBuf+(h+2)*buf_line_size+w)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			//	pBuf[h+2][w+8+width*4]
			*(pBuf+(h+2)*buf_line_size+w+8+width*4)= *(pTemp+(width-2)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//pBuf[h+2][w+8] 
			*(pBuf+(h+2)*buf_line_size+w+8)= *(pTemp+w+h*line_size);
		}
	}
	int val;
	int x,y;
	int xx,yy;
	int sx,sy;
	int ex,ey;
	int bx,by;
	int rr,gg,bb;
	int dum,dum2;
	int endn;
	int mm;
	int mlx,mly,zzx;
	int my1,my2;
	int col[3];
	int x1,y1,x2,y2;
	int msx,msy;
	x1=0;
	y1=0;
	x2=width-1;
	y2=height-1;
	msx=value/4;
	msy=value/4;
	if(msx==0) msx=1;
	if(msy==0) msy=1;
	mlx=(x2-x1+1+msx-1)/msx;
	mly=(y2-y1+1+msy-1)/msy;
	zzx=msx/2;
	for(y=0;y<mly;y++) {
		bx=0;
		for(x=bx;x<x2;x+=msx) {
			rr=gg=bb=0;
			sx=x;
			if(x<0) sx=0;
			sy=y1+y*msy;
			ex=x+msx-1;
			ey=sy+msy-1;
			if(ex>x2) ex=x2;
			if(ey>y2) ey=y2;
			for(yy=sy;yy<=ey;yy++) {
				for(xx=sx;xx<=ex;xx++) {
					//val = getPixel(img,xx,yy,&col); //æãÌæfîñðæŸ
					col[0]=*(pTemp+(xx)*4+(yy)*line_size);
					col[1]=*(pTemp+(xx)*4+(yy)*line_size+1);
					col[2]=*(pTemp+(xx)*4+(yy)*line_size+2); 
					rr+=col[0];
					gg+=col[1];
					bb+=col[2];
				}
			}
			mm=(ex-sx+1)*(ey-sy+1);
			if(mm) {
				rr/=mm;
				gg/=mm;
				bb/=mm;
				col[0] = rr;
				col[1] = gg;
				col[2] = bb;
				for(yy=sy;yy<=ey;yy++) {
					for(xx=sx;xx<=ex;xx++) {
						*(pTemp+(xx)*4+(yy)*line_size)=col[0];
						*(pTemp+(xx)*4+(yy)*line_size+1)=col[1];
						*(pTemp+(xx)*4+(yy)*line_size+2)=col[2];     // æÉlðZbg·é
					}
				}
			}
		}
	}
	
	
	//pTemp+=line_size*2+8;

	if(pBuf!=NULL)free(pBuf);
	return;
	
}

void iPhonePhotoCenter(unsigned char* pSrc,unsigned int width,unsigned int height,unsigned int ef){
	
	unsigned int line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;//[height+4][width*4+16];

	line_size=width*4;
	buf_line_size=width*4+16;
	pBuf=(unsigned char*)malloc(buf_line_size*(height+4));
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//			pBuf[h][w+8]
			*(pBuf+h*buf_line_size+w+8)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//			pBuf[h+height+2][w+8]
			*(pBuf+(h+height+2)*buf_line_size+w+8)= *(pTemp+w+(height-2+h)*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			//pBuf[h+2][w] 
			*(pBuf+(h+2)*buf_line_size+w)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			//	pBuf[h+2][w+8+width*4]
			*(pBuf+(h+2)*buf_line_size+w+8+width*4)= *(pTemp+(width-2)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//pBuf[h+2][w+8] 
			*(pBuf+(h+2)*buf_line_size+w+8)= *(pTemp+w+h*line_size);
		}
	}
	double rate;
	int i;
	int x,y;
	int xx,yy;
	int rr,gg,bb,oo;
	int col[3],ncol[3];
	
	int pat_sum,pat;
	double disI;
	double ox,oy;
	double disMAX;
	double dx,dy;
	int x1,y1,x2,y2;


	x1=0;
	y1=0;
	x2=width-1;
	y2=height-1;
	
	ox=(double)(x2-x1)/2.0;
	oy=(double)(y2-y1)/2.0;
	dx=(double)x2-ox;
	dy=(double)y2-oy;
	disMAX=sqrt(dx*dx+dy*dy);
	
	for(y=y1;y<=y2;y++) {
		for(x=x1;x<=x2;x++) {
			rr=gg=bb=oo=0;
			dx=(double)x;
			dy=(double)y;
			
			//dis=1000;//sqrt(dx*dx+dy*dy);
			rate=ef;//*dis/disMAX;
			rate/=((double)NF);
			pat_sum=0;
			for(i=0;i<NN;i++) {
				if(i==NF) pat=3;
				else pat=1;
				disI=(double)(i-NF)*rate;
				xx=(int)(disI)+x;
				yy=(int)(disI)+y;
				if(xx<2)xx=2;
				if(yy<2)yy=2;
				ncol[0]=*(pBuf+(xx)*4+(yy)*buf_line_size);
				ncol[1]=*(pBuf+(xx)*4+(yy)*buf_line_size+1);
				ncol[2]=*(pBuf+(xx)*4+(yy)*buf_line_size+2);       //æãÌæfîñðæŸ
				rr+=ncol[0] * pat;
				gg+=ncol[1] * pat;
				bb+=ncol[2] * pat;
				oo+= pat;
				pat_sum+=pat;
			}
			col[0] = rr/(oo);
			col[1] = gg/(oo);
			col[2] = bb/(oo);
			*(pTemp+(x)*4+(y)*line_size)=col[0];
			*(pTemp+(x)*4+(y)*line_size+1)=col[1];
			*(pTemp+(x)*4+(y)*line_size+2)=col[2];      // æÉlðZbg·é
		}
	}
	
	
	//pTemp+=line_size*2+8;
	
	if(pBuf!=NULL)free(pBuf);
	return;
	
}
void iPhonePhotoRotate(unsigned char* pSrc,unsigned int width,unsigned int height,unsigned int n){
	unsigned int line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;//[height][width*4];
	line_size=width*4;
	unsigned char temp;
	pBuf=(unsigned char*)malloc(height*line_size);
	//	buf_size=height*line_size;
	pTemp=pSrc;
	for(int x=0;x<n;x++){
		pBufTemp=(unsigned char*) pBuf;
		for(int w=0; w<(width); w++)
		{
	
			for(int h=height-1; h>=0; h--)
		{

			*(pBufTemp+0)= pTemp[(h)*line_size+w*4+0];
			*(pBufTemp+1)= pTemp[(h)*line_size+w*4+1];
			*(pBufTemp+2)= pTemp[(h)*line_size+w*4+2];
			*(pBufTemp+3)= pTemp[(h)*line_size+w*4+3];
			pBufTemp+=4;
		}
	}
	

	
	pTemp=pSrc;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			*(pTemp+w+h*line_size) =*(pBuf+w+h*line_size);
			
		}
	}
		temp=height;
		height=width;
		width=temp;
		line_size=width*4;
	}

	if(pBuf!=NULL)free(pBuf);
	
	
					   
					   
}
void iPhonePhotoFilterPastels(unsigned char* pSrc,unsigned int width,unsigned int height){
	unsigned int line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;//[height][width*4];
	line_size=width*4;

	pBuf=(unsigned char*)malloc(height*line_size);
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			*(pBuf+w+h*line_size)= *(pTemp+w+h*line_size);
		}
	}
	
	
	//iPhonePhotoPrewittFilter((unsigned char*)pBuf,width,height);
	
	//iPhonePhotoOverlayFilter((unsigned char*)pBuf,width,height);	
	//iPhonePhotoCenter((unsigned char*)pBuf,width,height,6);
	iPhonePhotoBrightFilter((unsigned char*)pBuf,width,height);	
	//iPhonePhotoOverlayFilter((unsigned char*)pBuf,width,height);	
	pTemp=pSrc;
	
	for(int h=0; h<(height); h++)
	{
		for(int w=(width*4)-4; w>=0; w-=4)
		{
			
			int x;
			if(w>40)x=5;
			else x=5-(40-w)/8;
			x-=rand()%5;
			
			(*(pBuf+w+h*line_size)) =  *(pBuf+w+h*line_size-x*4);
			(*(pBuf+w+h*line_size+1)) =  *(pBuf+w+h*line_size-x*4+1);
			(*(pBuf+w+h*line_size+2)) =  *(pBuf+w+h*line_size-x*4+2);
			(*(pBuf+w+h*line_size+3)) =  *(pBuf+w+h*line_size-x*4+3);
			
		
		}
	}
	

//	iPhonePhotoCenter((unsigned char*)pBuf,width,height,3);
	
	
	
	pTemp=pSrc;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w+=4)
		{
			if(rand()%100>(95-(h%5)*17)){
				*(pTemp+w+h*line_size) =(*(pBuf+w+h*line_size));
				*(pTemp+w+h*line_size+1) =(*(pBuf+w+h*line_size+1));
				*(pTemp+w+h*line_size+2) =(*(pBuf+w+h*line_size+2));
				*(pTemp+w+h*line_size+3) =(*(pBuf+w+h*line_size+3));
			}
		}
	}
	 

	if(pBuf!=NULL)free(pBuf);
	
	
}

void iPhonePhoto1to4(unsigned char* pSrc,unsigned int width,unsigned int height){
	unsigned int line_size;
	unsigned char* pTemp;

	line_size=width*4;
	

	
	//	buf_size=height*line_size;
	pTemp=pSrc;

	for(int h=0; h<(height)/2; h++)
	{
		for(int w=0; w<(width*4)/2; w+=4)
		{
			*(pTemp+w+h*line_size)= *(pTemp+w*2+h*2*line_size);
			*(pTemp+w+h*line_size+1)= *(pTemp+w*2+h*2*line_size+1);
			*(pTemp+w+h*line_size+2)= *(pTemp+w*2+h*2*line_size+2);
			*(pTemp+w+h*line_size+3)= *(pTemp+w*2+h*2*line_size+3);
		}
	}

	
	
	

	
	
}
void iPhonePhoto4to1(unsigned char* pSrc,unsigned int width,unsigned int height){
	unsigned int line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;
	line_size=width*4;
	
	pBuf=(unsigned char*)malloc(height*line_size);
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	
	
	for(int h=0; h<(height)/2; h++)
	{
		for(int w=0; w<(width*4)/2; w+=4)
		{
			*(pBufTemp+w*2+h*2*line_size)= *(pTemp+w+h*line_size);
			*(pBufTemp+w*2+h*2*line_size+1)= *(pTemp+w+h*line_size+1);
			*(pBufTemp+w*2+h*2*line_size+2)= *(pTemp+w+h*line_size+2);
			*(pBufTemp+w*2+h*2*line_size+3)= *(pTemp+w+h*line_size+3);
			
			*(pBufTemp+w*2+h*2*line_size+4)= *(pTemp+w+h*line_size);
			*(pBufTemp+w*2+h*2*line_size+1+4)= *(pTemp+w+h*line_size+1);
			*(pBufTemp+w*2+h*2*line_size+2+4)= *(pTemp+w+h*line_size+2);
			*(pBufTemp+w*2+h*2*line_size+3+4)= *(pTemp+w+h*line_size+3);
			
			*(pBufTemp+w*2+h*2*line_size+line_size)= *(pTemp+w+h*line_size);
			*(pBufTemp+w*2+h*2*line_size+1+line_size)= *(pTemp+w+h*line_size+1);
			*(pBufTemp+w*2+h*2*line_size+2+line_size)= *(pTemp+w+h*line_size+2);
			*(pBufTemp+w*2+h*2*line_size+3+line_size)= *(pTemp+w+h*line_size+3);
			
			*(pBufTemp+w*2+h*2*line_size+4+line_size)= *(pTemp+w+h*line_size);
			*(pBufTemp+w*2+h*2*line_size+1+4+line_size)= *(pTemp+w+h*line_size+1);
			*(pBufTemp+w*2+h*2*line_size+2+4+line_size)= *(pTemp+w+h*line_size+2);
			*(pBufTemp+w*2+h*2*line_size+3+4+line_size)= *(pTemp+w+h*line_size+3);
		}
	}
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			*(pTemp+w+h*line_size) = *(pBufTemp+w+h*line_size);
		}
	}
	if(pBuf!=NULL)free(pBuf);
	
	
}


void iPhonePhotoNeon(unsigned char* pSrc,unsigned int width,unsigned int height,double Value){
	unsigned int buf_size,line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;
	//[height+2][width*4+8];
	int sobelR,sobelG,sobelB;
	int cols_pixelR,cols_pixelG,cols_pixelB;
	int row_pixelR,row_pixelG,row_pixelB;
	int	hue, saturation, value;
	int temp;
	hue=(int)Value*3.6;
	/*
	ValueX=Value*255*255*255/100;
	sobelR=(int)ValueX/(255*255);
	sobelG=((int)ValueX/255)%255;
	sobelB=(int)ValueX%255;

	_RGB2HSV(sobelR,sobelG,sobelB,&hue,&saturation,&value);	

	*/
	
	line_size=width*4;
	buf_line_size=width*4+8;
	buf_size=height*line_size;
	pBuf=(unsigned char*)malloc((height+2)*buf_line_size);
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	for(int w=0; w<(width*4); w++)
	{
		//pBuf[0][w+4] =*(pTemp+w);
		*(pBuf+w+4)=*(pTemp+w);
	}
	
	
	for(int w=0; w<(width*4); w++)
	{
		//pBuf[height+1][w+4] =*(pTemp+w+(height-1)*line_size);
		*(pBuf+w+4+(height+1)*buf_line_size) =*(pTemp+w+(height-1)*line_size);
		
	}
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<4; w++)
		{
			//pBuf[h+1][w] = *(pTemp+w+h*line_size);
			*(pBuf+w+(h+1)*buf_line_size) =*(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<4; w++)
		{
			//	pBuf[h+1][w+4+width*4] =*(pTemp+(width-1)*4+w+h*line_size);
			*(pBuf+w+4+width*4+(h+1)*buf_line_size) =*(pTemp+(width-1)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//pBuf[h+1][w+4] = *(pTemp+w+h*line_size);
			*(pBuf+w+4+(h+1)*buf_line_size) =*(pTemp+w+h*line_size);
		}
	}
	
	
	pTemp=pSrc;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width); w++)
		{
 			row_pixelR = 0;
			row_pixelG = 0;
			row_pixelB = 0;
			// line - 1
			row_pixelR += *pBufTemp;
			row_pixelG += *(pBufTemp+1);
			row_pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
		 	row_pixelR += 2*(*pBufTemp);
			row_pixelG += 2*(*(pBufTemp+1));
			row_pixelB += 2*(*(pBufTemp+2));
			pBufTemp += 4;
			row_pixelR += *pBufTemp;
			row_pixelG += *(pBufTemp+1);
			row_pixelB += *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 2
			pBufTemp += 4;
			pBufTemp += 4;
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 3
			row_pixelR -= *pBufTemp;
			row_pixelG -= *(pBufTemp+1);
			row_pixelB -= *(pBufTemp+2);
		  	pBufTemp += 4;
		 	row_pixelR -= 2*(*pBufTemp);
			row_pixelG -= 2*(*(pBufTemp+1));
			row_pixelB -= 2*(*(pBufTemp+2));
			pBufTemp += 4;
			row_pixelR -= *pBufTemp;
			row_pixelG -= *(pBufTemp+1);
			row_pixelB -= *(pBufTemp+2);
			pBufTemp = pBufTemp - (buf_line_size << 1) - 8;
			
			cols_pixelR = 0;
			cols_pixelG = 0;
			cols_pixelB = 0;
			// line - 1
			cols_pixelR += *pBufTemp;
			cols_pixelG += *(pBufTemp+1);
			cols_pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
			pBufTemp += 4;
			cols_pixelR -= *pBufTemp;
			cols_pixelG -= *(pBufTemp+1);
			cols_pixelB -= *(pBufTemp+2);
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 2
			cols_pixelR += 2*(*pBufTemp);
			cols_pixelG += 2*(*(pBufTemp+1));
			cols_pixelB += 2*(*(pBufTemp+2));
		  	pBufTemp += 4;
			pBufTemp += 4;
			cols_pixelR -= 2*(*pBufTemp);
			cols_pixelG -= 2*(*(pBufTemp+1));
			cols_pixelB -= 2*(*(pBufTemp+2));
			pBufTemp = pBufTemp + buf_line_size - 8;
			// line - 3
			cols_pixelR += *pBufTemp;
			cols_pixelG += *(pBufTemp+1);
			cols_pixelB += *(pBufTemp+2);
		  	pBufTemp += 4;
			pBufTemp += 4;
			cols_pixelR -= *pBufTemp;
			cols_pixelG -= *(pBufTemp+1);
			cols_pixelB -= *(pBufTemp+2);
			pBufTemp = pBufTemp - (buf_line_size << 1) - 4;
			
			if(row_pixelR<0) row_pixelR *= (-1);
			if(row_pixelG<0) row_pixelG *= (-1);
			if(row_pixelB<0) row_pixelB *= (-1);
			if(cols_pixelR<0) cols_pixelR *= (-1);
			if(cols_pixelG<0) cols_pixelG *= (-1);
			if(cols_pixelB<0) cols_pixelB *= (-1);
			sobelR = (row_pixelR) + (cols_pixelR);
			sobelG = (row_pixelG) + (cols_pixelG);
			sobelB = (row_pixelB) + (cols_pixelB);
			//total += sobel;
			if(sobelR>50){
				sobelR=sobelR+25;
				sobelG=(sobelR-50)/2;
				sobelB=(sobelR-50)/2;	
			}
			else {
				sobelR=0;
				sobelG=0;
				sobelB=0;	
			}
			if(sobelR>255){
				sobelR=255;
				sobelG=205;
				sobelB=205;	
			}
			if(sobelR<0)sobelR=0;
			if(sobelG<0)sobelG=0;
			if(sobelB<0)sobelB=0;
			_RGB2HSV(sobelR,sobelG,sobelB,&temp,&saturation,&temp);	
			value = max(sobelR,max(sobelG,sobelB));
			_HSV2RGB(hue,saturation,value,&sobelR,&sobelG,&sobelB);
			*(pTemp+0) = sobelR;
			*(pTemp+1) = sobelG;
			*(pTemp+2) = sobelB;
			pTemp += 4;
		}
		pBufTemp += 8;
	} 
	//iPhonePhotoToneFilter(pSrc, width, height, 255, 0, 0);
	return;	
}

void iPhonePhotoHeatMapFilter(unsigned char* pSrc,unsigned int width,unsigned int height){
	unsigned int line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;
	double H,S,I,R,G,B;
	
	

	line_size=width*4;
	//	buf_size=height*line_size;
	pBuf=(unsigned char*)malloc((height)*line_size);
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//pBuf[h+2][w+8] 
			*(pBuf+h*line_size+w)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<height; h++)
	{
		for(int w=0; w<width; w++)
		{
			R=*pBufTemp;
			G= *(pBufTemp+1);
			B= *(pBufTemp+2);
			I=(R+G+B)/3;
			S=1;//-3/(R+G+B)*(R<G?(R<B?R:B):(G<B?G:B));
			H = ((2989*R)+(5866*G)+(1145*B))/10000;
			H =240-I;
			if(H<0)H=0;
			
			if(H<120){
	
				R=(1+S*cos(H*3.14/180)/cos((60-H)*3.14/180))/3;
			
				B=(1-S)/3;
				G=1-(R+B);
			}
			else if(H<240){
				H=H-120;
				R=(1-S)/3;
				G=(1+S*cos(H*3.14/180)/cos((60-H)*3.14/180))/3;
				B=1-(R+G);
			}
			else{
				H=H-240;
				G=(1-S)/3;
				B=(1+S*cos(H*3.14/180)/cos((60-H)*3.14/180))/3;
				R=1-(B+G);
			}
		
			*(pTemp+0) = 2*255*R;
			if(2*255*R>255)	*(pTemp+0)=255;
			if(2*255*R<0)	*(pTemp+0)=0;

			*(pTemp+1) = 2*255*G;
			if(2*255*G>255)	*(pTemp+1)=255;
			if(2*255*G<0)	*(pTemp+1)=0;
			*(pTemp+2) = 2*255*B;
			if(2*255*B>255)	*(pTemp+2)=255;
			if(2*255*B<0)	*(pTemp+2)=0;
			
			pTemp+=4;
			
				
			
			
			pBufTemp += 4;
		}
	}
	if(pBuf)free(pBuf);
	iPhonePhotoOverlayFilter(pSrc, width, height);

}

void iPhonePhotoOldMovie(unsigned char* pSrc,unsigned int width,unsigned int height,unsigned int value){
	unsigned int line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;

	

	line_size=width*4;
	//	buf_size=height*line_size;

	iPhonePhotoBrightFilter(pSrc, width, height);
	iPhonePhotoToneFilter((unsigned char*)pSrc,width,height, 230, 180,95);
	iPhonePhotoMeanFilter(pSrc, width, height);

	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w+=4)
		{
			if(rand()%(value+2)==0){
				if(*(pSrc+w+h*line_size)*1.4>255)*(pSrc+w+h*line_size)=255;
				else *(pSrc+w+h*line_size)*=1.4;
				if(*(pSrc+w+h*line_size+1)*1.4>255)*(pSrc+w+h*line_size+1)=255;
				else *(pSrc+w+h*line_size+1)*=1.4;
				if(*(pSrc+w+h*line_size+2)*1.4>255)*(pSrc+w+h*line_size+2)=255;
				else *(pSrc+w+h*line_size+2)*=1.4;
				if(*(pSrc+w+h*line_size+3)*1.4>255)*(pSrc+w+h*line_size+3)=255;
				else *(pSrc+w+h*line_size+3)*=1.4;
		
				
				
			}
			
		}
	}	
	
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w+=4)
		{
			if(rand()%(value+2)){
				if(*(pSrc+w+h*line_size)*1.2>255)*(pSrc+w+h*line_size)=255;
				else *(pSrc+w+h*line_size)*=1.2;
				if(*(pSrc+w+h*line_size+1)*1.2>255)*(pSrc+w+h*line_size+1)=255;
				else *(pSrc+w+h*line_size+1)*=1.2;
				if(*(pSrc+w+h*line_size+2)*1.2>255)*(pSrc+w+h*line_size+2)=255;
				else *(pSrc+w+h*line_size+2)*=1.2;
				if(*(pSrc+w+h*line_size+3)*1.2>255)*(pSrc+w+h*line_size+3)=255;
				else *(pSrc+w+h*line_size+3)*=1.2;
				
			
			}
		}
	}	
	
	pTemp=pSrc;
	int x,y,sumX,sumY,rgbSum;
	for(int h=0;h<height;h++){
		for(int w=0;w<width;w++){
			
			x=height/2-h;
			y=width/2-w;
			if(height/2<h)
			{
				x*=(-1);
			}
			if(width/2<w)
			{
				y*=(-1);   
			}
			
			sumY=400-y*255/(width/2);
			sumX=400-x*255/(height/2);
			if(sumY>255)sumY=255;
			if(sumX>255)sumX=255;
			if(sumY<100)sumY=sumY-(100-sumY);
			if(sumX<100)sumX=sumX-(100-sumX);
			if(sumY<0)sumY=0;
			if(sumX<0)sumX=0;
			rgbSum=255-(sumX+sumY)/2;
			
			//
			if(*pTemp+rgbSum>255)*pTemp=255;
			else *pTemp+=rgbSum;
			if(*(pTemp+1)+rgbSum>255)*(pTemp+1)=255;
			else *(pTemp+1)+=rgbSum;
			if(*(pTemp+2)+rgbSum>255)*(pTemp+2)=255;
			else *(pTemp+2)+=rgbSum;
						
			pTemp+=4;
		}
		
	}
	
	

}


void iPhonePhotoLv09Filter(unsigned char* pSrc,unsigned int width,unsigned int height,unsigned int value){
	
	unsigned int line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;//[height+4][width*4+16];
	
	line_size=width*4;
	buf_line_size=width*4+16;
	pBuf=(unsigned char*)malloc(buf_line_size*(height+4));
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//			pBuf[h][w+8]
			*(pBuf+h*buf_line_size+w+8)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//			pBuf[h+height+2][w+8]
			*(pBuf+(h+height+2)*buf_line_size+w+8)= *(pTemp+w+(height-2+h)*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			//pBuf[h+2][w] 
			*(pBuf+(h+2)*buf_line_size+w)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			//	pBuf[h+2][w+8+width*4]
			*(pBuf+(h+2)*buf_line_size+w+8+width*4)= *(pTemp+(width-2)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//pBuf[h+2][w+8] 
			*(pBuf+(h+2)*buf_line_size+w+8)= *(pTemp+w+h*line_size);
		}
	}

	double rate;
	int i;
	int x,y;
	int xx,yy;
	int rr,gg,bb,oo;
	int col[3],ncol[3];
	
	int pat_sum,pat;
	double rad,dis,disI; 
	double ox,oy;
	double disMAX;
	double dx,dy;
	int x1,y1,x2,y2;
	int ef=value;
	int nn=17;
	int nf=8;
	x1=0;
	y1=0;
	x2=width-1;
	y2=height-1;
	
	ox=(double)(x2-x1)/2.0;
	oy=(double)(y2-y1)/2.0;
	dx=(double)x2-ox;
	dy=(double)y2-oy;
	disMAX=sqrt(dx*dx+dy*dy);
	
	for(y=y1;y<=y2;y++) {
		for(x=x1;x<=x2;x++) {
			rr=gg=bb=oo=0;
			dx=(double)x-ox;
			dy=(double)y-oy;
			//
			if(dx!=0.0) rad=atan(dy/dx);
			else rad=3.14159265/2.0;
			dis=sqrt(dx*dx+dy*dy);
			rate=ef*dis/disMAX;
			rate/=((double)nf);
			pat_sum=0;

			

			for(i=0;i<nn;i++) {
				if(i==nf) pat=3;
				else pat=1;
				disI=(double)(i-nf)*rate;
				xx=(int)(disI*cos(rad))+x;
				yy=(int)(disI*sin(rad))+y; 
				if(xx<0)xx=0;
				if(yy<0)yy=0;
				if(xx<2&&yy<2)xx=2;
				
				if(xx>width+3)xx=width+3;
				if(yy>height+3)yy=height+3;
				if(xx<2&&yy>height+1)xx=2;
				if(xx>width+1<2&&yy<2)yy=2;
				if(xx>width+1<2&&yy>height+1)yy=height+1;
				ncol[0]=*(pBuf+(xx)*4+(yy)*buf_line_size);
				ncol[1]=*(pBuf+(xx)*4+(yy)*buf_line_size+1);
				ncol[2]=*(pBuf+(xx)*4+(yy)*buf_line_size+2);       //æãÌæfîñðæŸ
				rr+=ncol[0] * pat;
				gg+=ncol[1] * pat;
				bb+=ncol[2] * pat;
				oo+= pat;
				pat_sum+=pat;
			}
			col[0] = rr/(oo);
			col[1] = gg/(oo);
			col[2] = bb/(oo);
			*(pTemp+(x)*4+(y)*line_size)=col[0];
			*(pTemp+(x)*4+(y)*line_size+1)=col[1];
			*(pTemp+(x)*4+(y)*line_size+2)=col[2];      // æÉlðZbg·é
		}
	}
	
	
	//pTemp+=line_size*2+8;
	
	if(pBuf!=NULL)free(pBuf);
	return;
	
}
void iPhonePhotoLv10Filter(unsigned char* pSrc,unsigned int width,unsigned int height,unsigned int value){
	
	unsigned int line_size,buf_line_size;
	unsigned char* pTemp;
	unsigned char* pBufTemp;
	unsigned char* pBuf;//[height+4][width*4+16];
	
	line_size=width*4;
	buf_line_size=width*4+16;
	pBuf=(unsigned char*)malloc(buf_line_size*(height+4));
	//	buf_size=height*line_size;
	pTemp=pSrc;
	pBufTemp=(unsigned char*) pBuf;
	
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//			pBuf[h][w+8]
			*(pBuf+h*buf_line_size+w+8)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<2; h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//			pBuf[h+height+2][w+8]
			*(pBuf+(h+height+2)*buf_line_size+w+8)= *(pTemp+w+(height-2+h)*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			//pBuf[h+2][w] 
			*(pBuf+(h+2)*buf_line_size+w)= *(pTemp+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<8; w++)
		{
			//	pBuf[h+2][w+8+width*4]
			*(pBuf+(h+2)*buf_line_size+w+8+width*4)= *(pTemp+(width-2)*4+w+h*line_size);
		}
	}
	
	for(int h=0; h<(height); h++)
	{
		for(int w=0; w<(width*4); w++)
		{
			//pBuf[h+2][w+8] 
			*(pBuf+(h+2)*buf_line_size+w+8)= *(pTemp+w+h*line_size);
		}
	}
	
	double rate;
	int i;
	int x,y;
	int xx,yy;
	int rr,gg,bb,oo;
	int col[3],ncol[3];
	
	int pat_sum,pat;
	double rad,dis,disI; 
	double ox,oy;
	double disMAX;
	double dx,dy;
	int x1,y1,x2,y2;
	int ef=value;
	int nn=17;
	int nf=8;
	x1=0;
	y1=0;
	x2=width-1;
	y2=height-1;
	
	ox=(double)(x2-x1)/2.0;
	oy=(double)(y2-y1)/2.0;
	dx=(double)x2-ox;
	dy=(double)y2-oy;
	disMAX=sqrt(dx*dx+dy*dy);
	
	for(y=y1;y<=y2;y++) {
		for(x=x1;x<=x2;x++) {
			rr=gg=bb=oo=0;
			dx=(double)x-ox;
			dy=(double)y-oy;
			//
			if(dx!=0.0) rad=atan(dy/dx);
			else rad=3.14159265/2.0;
			rad+=(3.14159265/2.0);
			dis=sqrt(dx*dx+dy*dy);
			rate=ef*dis/disMAX;
			rate/=((double)nf);
			pat_sum=0;
			
			
			
			for(i=0;i<nn;i++) {
				if(i==nf) pat=3;
				else pat=1;
				disI=(double)(i-nf)*rate;
				xx=(int)(disI*cos(rad))+x;
				yy=(int)(disI*sin(rad))+y; 
				if(xx<0)xx=0;
				if(yy<0)yy=0;
				if(xx<2&&yy<2)xx=2;
				
				if(xx>width+3)xx=width+3;
				if(yy>height+3)yy=height+3;
				if(xx<2&&yy>height+1)xx=2;
				if(xx>width+1<2&&yy<2)yy=2;
				if(xx>width+1<2&&yy>height+1)yy=height+1;
				ncol[0]=*(pBuf+(xx)*4+(yy)*buf_line_size);
				ncol[1]=*(pBuf+(xx)*4+(yy)*buf_line_size+1);
				ncol[2]=*(pBuf+(xx)*4+(yy)*buf_line_size+2);       //æãÌæfîñðæŸ
				rr+=ncol[0] * pat;
				gg+=ncol[1] * pat;
				bb+=ncol[2] * pat;
				oo+= pat;
				pat_sum+=pat;
			}
			col[0] = rr/(oo);
			col[1] = gg/(oo);
			col[2] = bb/(oo);
			*(pTemp+(x)*4+(y)*line_size)=col[0];
			*(pTemp+(x)*4+(y)*line_size+1)=col[1];
			*(pTemp+(x)*4+(y)*line_size+2)=col[2];      // æÉlðZbg·é
		}
	}
	
	
	//pTemp+=line_size*2+8;
	
	if(pBuf!=NULL)free(pBuf);
	return;
	
}
void iPhonePhotoLv16Filter(unsigned char* pSrc, unsigned int width,unsigned int height,unsigned int value){
	unsigned int line_size,buf_line_size;
	unsigned char* pTemp;

	line_size=width*4;

	pTemp=pSrc;

	

	

	int x,y;
	int xx,yy;
	int hh;
	int count;
	int dr,dg,db;


	int rr,gg,bb;
	
	int col[3],ncol[3];
	int ans[3];
	int bufR[400],bufG[400],bufB[400];
	int ovx;
	int n2;
	int adr;
	int x1,y1,x2,y2;
	int nn,th;
	nn=7;
	th=value;
#define BUF_MX 20 	
	x1=0;
	y1=0;
	x2=width-1;
	y2=height-1;
	
	n2=nn*2+1;
	for(y=y1;y<=y2;y++) {
		x=x1;
		for(yy=(-nn);yy<=nn;yy++) {
			adr=(yy+nn)*BUF_MX;
			for(xx=(-nn);xx<nn;xx++) {
				if((x+xx>0&&x+xx<width)&&(y+yy>0&&y+yy<height)){
					ncol[0]=*(pSrc+(x+xx)*4+(y+yy)*line_size);
					ncol[1]=*(pSrc+(x+xx)*4+(y+yy)*line_size+1);
					ncol[2]=*(pSrc+(x+xx)*4+(y+yy)*line_size+2);
				}else
				{
					ncol[0]=0;
					ncol[1]=0;
					ncol[2]=0;
				}
				
				//val = getPixel(img,x+xx,y+yy,&ncol);    //æãÌæfîñðæŸ
				bufR[adr]=ncol[0];
				bufG[adr]=ncol[1];
				bufB[adr]=ncol[2];
				adr++;
			}
		}
		ovx=n2-1;
		for(x=x1;x<=x2;x++) {
			xx=nn;
			for(yy=(-nn);yy<=nn;yy++) {
				adr=(yy+nn)*BUF_MX+ovx;
				//	val = getPixel(img,x+xx,y+yy,&ncol);    //æãÌæfîñðæŸ
				if((x+xx>0&&x+xx<width)&&(y+yy>0&&y+yy<height)){
					ncol[0]=*(pSrc+(x+xx)*4+(y+yy)*line_size);
					ncol[1]=*(pSrc+(x+xx)*4+(y+yy)*line_size+1);
					ncol[2]=*(pSrc+(x+xx)*4+(y+yy)*line_size+2);
				}else
				{
					ncol[0]=0;
					ncol[1]=0;
					ncol[2]=0;
				}
				
				//val = getPixel(img,x+xx,y+yy,&ncol);    //æãÌæfîñðæŸ
				bufR[adr]=ncol[0];
				bufG[adr]=ncol[1];
				bufB[adr]=ncol[2];				
				adr++;
			}
			ovx=(ovx+1)%n2;
			rr=gg=bb=0;
			count=0;
			col[0]=*(pSrc+(x)*4+(y)*line_size);
			col[1]=*(pSrc+(x)*4+(y)*line_size+1);
			col[2]=*(pSrc+(x)*4+(y)*line_size+2);
			//val = getPixel(img,x+xx,y+yy,&ncol);    //æãÌæfîñðæŸ
			//æãÌæfîñðæŸ
			for(yy=0;yy<n2;yy++) {
				adr=yy*BUF_MX;
				for(xx=0;xx<n2;xx++) {
					dr=absi((int)(col[0]) - bufR[adr]);
					dg=absi((int)(col[1]) - bufG[adr]);
					db=absi((int)(col[2]) - bufB[adr]);
					if(dr<th && dg<th && db<th) {
						rr+= bufR[adr];
						gg+= bufG[adr];
						bb+= bufB[adr];
						count++;
					}
					adr++;
				}
			}
			if(count) {
				hh=rr/count;
				ans[0] = hh;
				hh=gg/count;
				ans[1] = hh;
				hh=bb/count;
				ans[2] = hh;
			}
			else {
				ans[0]=col[0];
				ans[1]=col[1];
				ans[2]=col[2];
			}
			*(pTemp+(x)*4+(y)*line_size)=ans[0];
			*(pTemp+(x)*4+(y)*line_size+1)=ans[1];
			*(pTemp+(x)*4+(y)*line_size+2)=ans[2];     // æÉlðZbg·é
		}
	}
	
	
}

void _RGB2HSV(int red, int green, int blue, int * hue, int * saturation, int * value)
{
	int		max_value, min_value;
	int		max_channel, min_channel;		// 1=red, 2=green, 3=blue
	
	
	max_value = max(red,max(green,blue));
	min_value = min(red,min(green,blue));
	if( max_value==red ) max_channel = 1;
	else if( max_value==green ) max_channel = 2;
	else if( max_value==blue ) max_channel = 3;
	if( min_value==red ) min_channel = 1;
	else if( min_value==green ) min_channel = 2;
	else if( min_value==blue ) min_channel = 3;
	// HUE
	if( max_value==min_value ){
		*hue = 0;
	}else{
		switch( max_channel ){
			case 1:
				if( green==blue ) *hue = 0;
				else *hue = (60*(int)(green-blue)/(int)(max_value-min_value)+360) % 360;
				break;
			case 2:
				if( blue==red ) *hue = 120;
				else *hue = (60*(int)(blue-red)/(int)(max_value-min_value)+120);
				break;
			case 3:
				if( red==green ) *hue = 240;
				else *hue = (60*(int)(red-green)/(int)(max_value-min_value)+240);
				break;
		}
	}
	// SATURATION
	if( max_value==0 ){
		*saturation = 0;
	}else{
		*saturation = (float)(max_value-min_value) / (float)max_value * (float)255.0f;
	}
	// VALUE
	*value = max_value;
}
void _HSV2RGB(int hue, int saturation, int value, int * red, int * green, int * blue)
{
	float		f, p, q, t, h, s, v;
	float		i;
	
	h = (float)hue;
	if( saturation==0 ){
		*red = value;
		*green = value;
		*blue = value;
		return;
	}else{
		s = (float)saturation/255.0f;
	}
	if( value==0 ){
		v = 0.0f;
	}else{
		v = (float)value/255.0f;
	}
	
	h = h / 60.0f;
	i = floor(h);
	f = h - i;
	p = v * (1.0f-s);
	q = v * (1.0f-s*f);
	t = v * (1.0f-s*(1.0f-f));
	
	switch( (int)i )
	{
		case 0:
			*red = v*255;
			*green = t*255;
			*blue = p*255;
			break;
		case 1:
			*red = q*255;
			*green = v*255;
			*blue = p*255;
			break;
		case 2:
			*red = p*255;
			*green = v*255;
			*blue = t*255;
			break;
		case 3:
			*red = p*255;
			*green = q*255;
			*blue = v*255;
			break;
		case 4:
			*red = t*255;
			*green = p*255;
			*blue = v*255;
			break;
		case 5:
		default:
			*red = v*255;
			*green = p*255;
			*blue = q*255;
			break;
	}
}